Jquery.makearray () function explanation

Source: Internet
Author: User

jQuery.makeArray()The function is used to convert a class array object to a true array object .

The so-called "class Array Object" is a regular object object, but it is very similar to and array objects: With the Length property, and with 0, 1, 2, 3 ... Equal number as the property name.

However, it is not an array after all, there is no built-in method inherited from the array's prototype object (for example: push (), sort (), and so on).

Jquery.makearray (object)

Precautions:

    • A class array object, which should have at least the length property, even if its value is 0, it can have no "element" (equivalent to an empty array).
    • If object the parameter does not have a length property, it is not a class array object. jQuery.makeArray()It is directly treated as an element in the result array.
    • A string object has a length property, but it is not generally treated as a class array object. The function still directly sees it as an element in the result array.
    • If the maximum number attribute of an object is greater than or equal to the length property, the number property that is greater than or equal to its value is ignored, whichever is the length property.
return value

jQuery.makeArray()The return value of the function is the array type, returning the converted array object.

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

Class array objects are very common, such as jquery objects that we use frequently,nodelist objects , and arguments objects within functions , all of which are class array objects. They all have the length property, and the corresponding element or parameter is accessed through a numeric property. However, they are not real array objects after all, so they cannot use the built-in methods of the array. Through jQuery.makeArray() functions, we can convert a class array object to a real array object, using the built-in method of the Array object.

Chestnuts:

//appends a newline label and the specified HTML content to the current pagefunctionW (HTML) {//Document.body.innerHTML + = "<br/>" + html;Console.log (HTML);}varobj = {    0: "Codeplayer",    1: "Hello",    2:18,    3:true};obj.length= 4;//class Array object is not a true arrayW (objinstanceofArray);//falsevararr =$.makearray (obj); W (arrinstanceofArray);//trueW (Arr.join (""));//Codeplayer Hello + TrueW (arr.length);//4varObj2 ={};obj2[2] = "DIY"; Obj2.length= 1;varARR2 =$.makearray (OBJ2); W (arr2instanceofArray);//trueW (arr2.length);//1varObj3 ={};obj3.length= 0;varARR3 =$.makearray (OBJ3); W (Arr3instanceofArray);//trueW (arr3.length);//0varObj4 = {};//because OBJ4 does not have the length property, it is converted directly to an element in the result arrayvarARR4 =$.makearray (OBJ4); W (ARR4instanceofArray);//trueW (arr4.length);//1W (Arr4[0]);//[Object Object]//Although the string has the length property, it is still treated as an element in the result arrayvarARR5 = $.makearray ("Codeplayer"); W (ARR5instanceofArray);//trueW (arr5.length);//1W (Arr5[0]);//Codeplayer

Our common jquery objects, nodelist objects, and arguments objects are also class array objects.

//The jquery object is also a class array objectvar$p = $ ("p");varARR1 =$.makearray ($p); W ($pinstanceofArray);//falseW (arr1instanceofArray);//true//The NodeList object is also a class array objectvarp = document.getelementsbytagname ("P");varARR2 =$.makearray (P); W (PinstanceofArray);//falseW (arr2instanceofArray);//truefunctionFoo (A, b) {//The arguments object is also a class array object    varARR3 =$.makearray (arguments); W (ArgumentsinstanceofArray);//falseW (ARR3instanceofArray);//true}foo (1, 2);

Excerpt from: http://www.365mini.com/page/jquery_makearray.htm

Jquery.makearray () function explanation

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.