Turn object (with Length property) array Array.prototype.slice.call (arguments)

Source: Internet
Author: User

We know that Array.prototype.slice.call (arguments) can convert an object with the length attribute to an array, except for the node collection under IE (because the DOM object under IE is implemented as a COM object, JS objects and COM objects cannot be converted) such as:
var a={length:2,0: ' First ', 1: ' Second '};  2 Array.prototype.slice.call (a); // ["First", "second"]var a={length:2};  5 Array.prototype.slice.call (a); // [Undefined, undefined]        

Probably just started to learn JS's children's shoes do not quite understand why this can achieve such a function. For example, I am a, so, to explore.

First of all, slice has two usages, one is String.slice, the other is Array.slice, the first one returns the string, the second is the array, and here we see 2nd.

Array.prototype.slice.call (arguments) is able to convert arguments to a group, then Arguments.toarray (). Slice (); Here, is it possible to say Array.prototype.slice.call (arguments) is the process of first passing in the first parameter into an array, and then call slice? Then look at the use of call, the following example
 1 var a = function< Span style= "color: #000000;" > () {2 Console.log (this); // littledu ' 3 Console.log (typeof this); // Object4 Console.log (this instanceof String); // True5 }6 a.call (' littledu ');          
As can be seen, call after the current function pushed into the scope of the arguments passed, do not know that this is right, but anyway this point to the object passed in is certain. Here, the basic is almost, we can boldly guess the internal implementation of slice, as follows
1 Array.prototype.slice =function(Start,end) {2var result =new Array ();  3 start = Start | | 0; 4 End = End | | this.length; //this points to the invoked object, which, when called, can change the direction of this, that is, the object that is passed in, This is the key 5 for (var i = start; I < end; I++6 Result.push ([i]); 7 }8 return Result; 9}                 

This is probably the case, understanding on the line, do not delve into.

Finally, a general function that is attached to an array of

1var ToArray =function(s) {2Try{3Return Array.prototype.slice.call (s);  4} catch (e) { 5 var arr = []; 6 for (var i = 0,len = s.length; i < Len; I++) { 7 //arr.push (S[i]); 
Arr[i] = s[i];//This is said to be faster than push 8 Span style= "color: #000000;" >} 9 return Arr;10 }11}

Turn object (with Length property) array Array.prototype.slice.call (arguments)

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.