One of the things that happens a lot in front end development is you take a pseudo-array and you turn it into an array

var fackArr = {
0:'abc',
1:'def',
2:'igk',
length: 3
}
Copy the code

It usually goes something like this:

var arr = [].slice.call(fackArr)
Copy the code

I get the actual array, but I don’t understand why I wrote it that way.

To understand how this works, we need to understand how slice works. Here we define a mySlice method:

Array.prototype.mySlice = function(){
  var start = 0;
  var end = this.length
  if (arguments.length === 1) {
    start = arguments[0]
  } else if (arguments.length === 2) {
    start = arguments[0]
    end = arguments[1]
  }

  var tem = []
  for (var i = start; i < end; i++) {
    tem.push(this[i])
  }
  return tem;
}

var fackArr = {
  0: 'abc',
  1: 'def',
  2: 'ghi',
  length: 3
}

var realArr =  [].mySlice.call(fackArr)
console.log(realArr)// ['abc', 'def', 'ghi']
Copy the code

The slice method takes two optional arguments, start and end. If no arguments are passed, the slice method defaults to the 0th bit and continues to the last bit. If a parameter is passed, it is truncated from start to length-1 bit; If you pass two arguments, starting at start and ending at end, without ending at end, and return a new array, and call changes this, tem.push(this[I]) will fetch the value of I in the pseudo-array. Put it into a temporary array TEM to get the real array we want.