JS class array of arrays (Array.prototype.slice.call (arraylike,0))

Source: Internet
Author: User

Welcome to my personal blog Xiao Dong's blog

The Array.prototype.slice.call (arraylike,0) method can be used to convert an array of class arrays to facilitate operation in JS what is an array of classes.

var nodelist=document.queryselectorall ("div");

This nodelist is an array of classes.

var nodeList2 = document.getElementById (' ul1 '). ChildNodes;

This nodeList2 is also an array of classes.
We can use nodelist2[0] to fetch the first child element. But when we return false with Console.log (NodeList2 instanceof Array) That means it is not an instance of an array, it is not an array. converting an array through Array.prototype.slice.call (arraylike,0)

Array.prototype.slice represents the slice method in the prototype of an array. Note that this slice method returns an array-type object;

Although the arraylike surface is represented as an array, there is virtually no function of the native array slice, where the call () method is used to modify the function of an incomplete array of Arraylike objects.

var Nodearray = Array.prototype.slice.call (nodelist,0);

var nodeArray2 = Array.prototype.slice.call (nodelist2,0);

At this point, the return of True by calling Console.log (nodeArray2 instanceof Array) indicates that it has been converted to an array object. IE8 and earlier browsers node-type nodes problems

Because IE8 and earlier browsers implemented NodeList as a COM object, we could not use the document.getElementById (' ul1 ') as we would use JavaScript objects. ChildNodes this way get Nod Elist object. Therefore, using Array.prototype.slice.call (nodelist2,0) in IE8 and earlier browsers will cause an error, you must enumerate all the members manually, and the following code is written in all browsers that are compatible

function Converttoarray (nodes) {
    var array = null;
    try{
        array = Array.prototype.slice.call (nodes,0);
    } catch (ex) {
        array = new Array ();
        for (var i=0; len=nodes.length; i<len; i++) {
            array.push (nodes[i]);
        }

    return array;
}

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.