Array.prototype.slice.call()

For MDN in Array.prototype.slice.()the introduction, the array-mentioned objects. The following is the text:

sliceThe method can be used to convert a class array (Array-like) object / collection to a new array. You only need this method to bind to this object. It is a function argumentsthat is an example of an array of objects class.

function list() {
  return Array.prototype.slice.call(arguments); } var list1 = list(1, 2, 3); // [1, 2, 3]

In addition to use Array.prototype.slice.call(arguments), you can simply use [].slice.call(arguments)instead.

So arguments is not a real array object, just like arrays only, so it does not slice this method, while Array.prototype.slice.call (arguments, 1) is to be understood as arguments converted into an array of objects, let arguments having a slice () method.

Similarly it shows that we can to Array.prototype.slice.call(arguments)add the second parameter.

function list() {
  return Array.prototype.slice.call(arguments, 1); } var list2 = list(4, 5, 6); list2; //[5, 6]
转载自:https://www.cnblogs.com/xingteng/p/9878523.html

Guess you like

Origin www.cnblogs.com/planetwithpig/p/11524967.html