Discussion [] .slice.call (arguments, 1)

Today when debugging the code tracking project, we found use such a passage [] .slice.call in a function in (arguments, 1) code. Hearts they got suspicious, and call for a slice of the method, it touches very strange, resolved under the above part of the code, is to get arguments from the beginning to the end of all the elements. Just wondering why not just use this to go with arguments.slice (1) it? (PS: when JS is eating had a "JavaScript The Definitive Guide", but later did not go in depth with how recently transferred to the development of HTML5 Game, only to find a lot of basic things are forgotten about, sweat !!!). So, on their own choosing to the following tests:

Test 1 First print the next arguments

var a = function(f){
console.log(arguments);
}

a('show', [12,3,4,55]);

 

Results: [ 'show', Array [ 4]]

Test 2

var a = function(f){
console.log([].slice.call(arguments, 1));
}

a('show', [12,3,4,55]);

 

Results: [Array [4]]
Test 3

var a = function(f){
console.log(arguments.slice(1));
}

a('show', [12,3,4,55]);

Results: The error! ! ! ** arguments.slice is not a function (... ) **
this time on the hearts of the doubt, and why in a test print out the arguments is an array of things, ah, why would not suggest this approach slice it ? With questions I have to ask at the ranks of Daniel, we got the following answer:

Test 4

(function() {
console.log(arguments instanceof Array)
})();

Results: to false
arguments array not just a single parameter access and the same access array elements. Therefore, when using the slice method requires a similar [] .slice.call (arguments, 1) in such a way to call, at this point, thinking about this statement triggered and it has ended

 

 

 

 

 

.

Guess you like

Origin www.cnblogs.com/jianxian/p/11997238.html