Array.prototype.slice.call (arguments) understood popular method

Original link: https://www.mk2048.com/blog/blog.php?id=h0cb2b2ciccb&title=Array.prototype.slice.call%28arguments%29+%E9%80%9A%E4%BF%97%E6% B3% 95% E7% 90% 86% E8% A7% A3
Array.prototype.slice.call (arguments, num)  an object having a length property can turn into an array.
 
slice literally meaning can be understood as part of the array taken.
call  literally be understood as meaning interception
arguments just like arrays, not the real array object, so it does not slice this way for arguments converted into an array of objects, so arguments with a slice () method.
Note: If direct write arguments.slice (num) error.
Then 
Array.prototype.slice.call(arguments,num); 
That is the argument of the method call interception. 
Such as:
var a={length:2,0:'first',1:'second'};//类数组,有length属性,长度为2,第0个是first,第1个是second
console.log(Array.prototype.slice.call(a,0));// ["first", "second"],调用数组的slice(0);

var a={length:2,0:'first',1:'second'};
console.log(Array.prototype.slice.call(a,1));//["second"],调用数组的slice(1);

var a={0:'first',1:'second'};//去掉length属性,返回一个空数组
console.log(Array.prototype.slice.call(a,0));//[]
function test(){
  console.log(Array.prototype.slice.call(arguments,0));//["a", "b", "c"],slice(0)
  console.log(Array.prototype.slice.call(arguments,1));//["b", "c"],slice(1)
}
test("a","b","c");
 

More professional front-end knowledge, make the [2048] ape www.mk2048.com

Guess you like

Origin blog.csdn.net/QDY5945/article/details/102765061