Home > Web Front-end > JS Tutorial > Detailed explanation on the use of .makeArray() function in jQuery

Detailed explanation on the use of .makeArray() function in jQuery

黄舟
Release: 2017-07-19 09:22:31
Original
1486 people have browsed it

jQuery.makeArray()The function is used to convert an array-like object to a real Array object .

The so-called "array-like object" is a regular Object object, but it is very similar to an array object: it has a length attribute and numbers such as 0, 1, 2, 3... as attribute name.

But it is not an array after all, and there are no built-in methods inherited from the prototype object of the array (for example: push(), sort(), etc.).


jQuery.makeArray( object )
Copy after login

Note:

  • An array-like object, it should have at least length Attribute, even if its value is 0, it may have no "elements" (equivalent to an empty array).

  • If the parameter object does not have a length attribute, it is not an array-like object. jQuery.makeArray() will be treated directly as an element in the result array.

  • Although String objects have a length attribute, they are generally not regarded as array-like objects. The function still treats it directly as an element in the result array.

  • If the object's largest numeric attribute is greater than or equal to the length attribute, the length attribute shall prevail, and numeric attributes greater than or equal to its value will be ignored.

Return value

jQuery.makeArray()The return value of the function is of Array type and returns the converted array object.

----------------------------------------- --------------------------------------------------

Array-like objects are very common, such as the jQuery objects, NodeList objects and arguments objects within functions that we often use , are all array-like objects. They all have a length attribute, and the corresponding elements or parameters are accessed through numeric attributes. However, they are not real array objects after all, so they cannot use the built-in methods of the array counterpart. Through the jQuery.makeArray() function, we can convert the array-like object into a real array object, thereby using the built-in methods of the array object.

Chestnut:


//在当前页面内追加换行标签和指定的HTML内容function w(html) {    //document.body.innerHTML += "<br/>" + html;    console.log(html);
}var obj = {    0: "CodePlayer",    1: "Hello",    2: 18,    3: true};
obj.length = 4;// 类数组对象不是真正的数组w(obj instanceof Array); // falsevar arr = $.makeArray(obj);
w(arr instanceof Array); // truew(arr.join(" ")); // CodePlayer Hello 18 truew(arr.length); // 4var obj2 = {};
obj2[2] = "DIY";
obj2.length = 1;var arr2 = $.makeArray(obj2);
w(arr2 instanceof Array); // truew(arr2.length); // 1var obj3 = {};
obj3.length = 0;var arr3 = $.makeArray(obj3);
w(arr3 instanceof Array); // truew(arr3.length); // 0var obj4 = {};// 由于obj4没有length属性,直接将其转换为结果数组中的一个元素var arr4 = $.makeArray(obj4);
w(arr4 instanceof Array); // truew(arr4.length); // 1w(arr4[0]); // [object Object]// 虽然字符串有length属性,但它仍被视作结果数组中的一个元素var arr5 = $.makeArray("CodePlayer");
w(arr5 instanceof Array); // truew(arr5.length); // 1w(arr5[0]); // CodePlayer
Copy after login

Our commonly used jQuery objects, NodeList objects, and arguments objects are also array-like objects. .


// jQuery对象也是一个类数组对象var $p = $("p");var arr1 = $.makeArray( $p );
w( $p instanceof Array ); // falsew( arr1 instanceof Array ); // true// NodeList对象也是一个类数组对象var p = document.getElementsByTagName("p");var arr2 = $.makeArray( p );
w( p instanceof Array ); // falsew( arr2 instanceof Array ); // truefunction foo(a, b){    // arguments对象也是一个类数组对象
    var arr3 = $.makeArray( arguments );
    w( arguments instanceof Array ); // false   
    w( arr3 instanceof Array ); // true}

foo(1, 2);
Copy after login


The above is the detailed content of Detailed explanation on the use of .makeArray() function in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template