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:
1 var a={length:2,0: ' First ', 1: ' Second '};2 Array.prototype.slice.call (a);//  ["First", "second"]3  4 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 turn arguments into a group, then Arguments.toarray (). Slice (); here it is. Is it possible to say that the process of Array.prototype.slice.call (arguments) is to convert the first parameter passed in to an array before calling slice? Then look at the use of call, the following example
1 var a = function () {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) {2      var 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, to point to the incoming object, which 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.

1 function arrayof () {2   return [].slice.call (arguments); 3 }

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

1 var toArray = function (s) {2     try{3         return 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 } 9 return arr;10 }11}

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.