Converts a class array object (Array-like object) to a group object (array objects)

Source: Internet
Author: User

Usage: Array.prototype.slice.call ( array-like object )

// creating an array of classes object var alo = {0: "A", 1: "B", 2: "C", Length:3}; // Conversions var arr = Array.prototype.slice.call (alo); Console.log (    //  false) Console.log (    //  true/ / {' 0 ': ' A ', ' 1 ': ' B ', ' 2 ': ' C ', Length:3}//  [' A ', ' B ', ' C ']

The following is an analysis of how it works.

A class Array object has an array-like structure, so it can use most of the array's methods.

There is a method in the array slice (), iterating over the calling object, and returning an array.

Here's a question, slice () is the method of the array object, how to use it?

An array method is a property of an array object, so it can be obtained by Array.prototype.slice .

However, the method of invoking other objects in an object, we need to use the "function.prototype" method called Call (), like this Function name.call (Obj,args).

So there,Array.prototype.slice.call (ALO)

Slice () is a function, so the method call is inherited from Function.prototype , which is why we can use Array.prototype.slice.call ( ...)

The first parameter of Function.prototype.call (obj) is an object that is about to be passed to F in the f . Call ( obj ) function, and this step, the value of F in the context (keyword this) will be obj.

In general Array.slice (), method Slice uses an array as the value of this and iterates through all the array elements using an index. In our example, we set Alo to the value of this, Alo is an array of classes, which itself has an index, and therefore works.

Nodelist,htmlcollection are class array objects

<body><ul id= "ho" class= "ho" > <li></li></ul><script>//Create a Htmlcollection objectvarHtmlcollection = document.getElementsByTagName ("li");//Create a NodeList objectvarNodeList = document.getElementById ("Ho"). ChildNodes;//ConversionsvarH2arr =Array.prototype.slice.call (htmlcollection);varN2arr =Array.prototype.slice.call (nodeList); Console.log (htmlcollection);//htmlcollection [<li>]Console.log (nodeList);//NodeList [#text "", <li>, #text ""]Console.log (H2arr);//Array [<li>]Console.log (N2arr);//array[#text "", <li>, #text ""]</script></body>

Converts a class array object (Array-like object) to a group object (array objects)

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.