"Notes" JS Array.prototype.slice.call (arguments) transforms the arguments of a function into an insight into an array method

Source: Internet
Author: User

We know that the argument inside the function is actually an object stored as an array, but it's not an array.

If we are going to convert it to an array, you can call Array.prototype.slice () this method

Analyze This method:

array.prototype: Array is actually a class name, but the method inside the call class can only be called through an instance object of the class, so here is a array.prototype called by its own prototype object.

Next is slice (): This is an array class inside a method function is to intercept a part of the array content, it receives two parameters slice (' array subscript start position ', ' array subscript end position ')

Call () method: Put a method into another execution environment, this method also has two parameters called (' Execution Environment ', ' to invoke the method of the parameter (can be ignored) ')

Well, look at the example:

functionConverttoarray (nodes) {vararr =NULL; //compatible with IE          Try{arr= Array.prototype.slice.call (nodes,0); //because slice is a method of the array class             //and the passed in parameter is an array of Dom objects             //so the method of array class is put into this object by the call method to execute             //The second argument to call is to pass in a parameter to the called method             //here, 0 means starting at index 0.}Catch(ex) {arr=NewArray ();  for(vari = 0; i < nodes.length; i++) {Arr.push (nodes[i]); }                        }          returnarr; }

This is an example of elevation: passing a function into an ChildNodes instance object, and then converting the object of this child element model to an array

The ChildNodes property contains a NodeList object, NodeList is a class array object that holds an ordered set of nodes that can be accessed by location

To be compatible with ie<9 subclasses are COM objects we cannot use such objects as JScript objects, so in catch we iterate through the NodeList object inside the ChildNodes property and instantiate an array Save these traversed node elements

"Notes" JS Array.prototype.slice.call (arguments) transforms the arguments of a function into an insight into an array method

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.