Developing advanced Tutorials Using Dojo's AJAX applications, part 6th: An in-depth introduction to Dojo basic libraries

Source: Internet
Author: User
Tags filter foreach arrays contains join

The Dojo Base library (dojo base), the core of the dojo framework, contains the core content associated with Ajax application development and is the foundation of the Dojo Core library (Dojo cores), the Dojo User Interface Library (DIJIT), and the Dojo Extension Library (DOJOX). Dojo Basic Library contains more content, the following will be a detailed description of each of these modules. Content related to DOM queries and operations and event handling is already covered in other articles in this series, and is not discussed here. And the object-oriented content will be introduced in other articles. The following first describes the accessibility methods that are included in the Dojo base library.

Assistive Tools Methods

In Ajax application development, it is often necessary to write some tools to assist development to reduce code duplication. The Dojo base library has provided a complementary approach to some of the features that are often used in development. Use these methods to improve development efficiency and code quality.

Array processing

Array processing is a common practice in Ajax application development. The Dojo Base library provides methods to facilitate the processing of arrays and perform some typical tasks. The specific descriptions of these methods associated with array processing are as follows:

Dojo.foreach (Array, callback, scope): This method is used to traverse an array and perform operations on each of these elements. Its parameter array represents an array, and callback represents the JavaScript method that performs the action on each element, and the optional scope represents the object to which this is pointed when the callback method call. When invoked, the callback method passes in three arguments, representing the current element, the ordinal number of the current element in the array, and the array itself.

Dojo.every (Array, callback, scope): This method is used to determine whether all elements of an array satisfy a particular condition. Its three parameters have the same meaning as the Dojo.foreach () method. The callback method declares whether an element meets the criteria by returning a true or false value.

Dojo.some (Array, callback, scope): This method is used to determine whether at least one element of an array satisfies a particular condition. The three parameters and the callback method have the same meaning as dojo.every ().

Dojo.map (Array, callback, scope): This method is used to perform operations on each element in an array, and returns the result of an arrays containing operations. Its three parameters have the same meaning as Dojo.foreach ().

Dojo.filter (Array, callback, scope): This method is used to filter the elements contained in the array, retaining only those elements that meet certain conditions. Its three parameters have the same meaning as Dojo.foreach (). The callback method declares whether an element should be preserved by returning a true or false value.

Dojo.indexof (array, value, Fromindex, FindLast): This method is used to find the specified element in the array and, if found, returns the ordinal number of the element in the array, or 1. Its parameter array represents an array, value represents the element value to find, Fromindex represents the starting ordinal position of the lookup, and FindLast indicates whether to start the search from the end of the array.

An example of how to use the above method is given in Listing 1.

Listing 1. Examples of how Dojo basic library arrays are handled

var array = [2, 4, 6, 8, 10];
  dojo.forEach(array, function(number, i) {
   alert("第" + (i + 1) + "个数是:" + number);
  });
  var allEven = dojo.every(array, function(number) {
   return number % 2 == 0;
  });
  if (allEven) {
   alert("数组中全部是偶数。");
  }
  var hasTrimerous = dojo.some(array, function(number) {
   return number % 3 == 0;
  });
  if (hasTrimerous) {
   alert("数组中包含 3 的倍数。");
  }
  var tenTimes = dojo.map(array, function(number) {
   return number * 10;
  });
  alert("数组中所有元素乘以 10:" + tenTimes.join(","));
  var lessThanFive = dojo.filter(array, function(number) {
   return number < 5;
  });
  alert("数组中小于 5 的元素:" + lessThanFive.join(","));
  alert("数组中 6 的序号是:" + dojo.indexOf(array, 6));

After describing how the arrays are handled in the Dojo base library, the following describes the processing of JavaScript methods.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.