[] .push.apply如何工作?

时间:2017-07-18 12:58:40

标签: javascript recursion apply

有人可以解释一下这行代码是如何运作的。

[].push.apply(perms, permutation(arr.slice(0), start + 1, last));

此函数生成一个包含输入数组的所有排列的数组;

var permutation = function(arr, start, last){
  var length = arr.length;

  if(!start){
    start = 0;
  }

  if(!last){
    last = length - 1;
  }

  if( last === start){
    return [arr];
  }

  var temp;
  var perms = [];

  for(var i = start; i < length; i++){
    swapIndex(arr, i, start);
    console.log(arr);

    [].push.apply(perms, permutation(arr.slice(0), start + 1, last)); 

    swapIndex(arr, i, start);
  }

  return perms;
};

2 个答案:

答案 0 :(得分:3)

[].push创建一个新数组,然后提取push,它与Array.prototype.push相同,但每次需要进行垃圾回收时都会创建一个未使用的对象。

如果你致电Array.prototype.push(5)它将无法正常工作,因为this不会被设置为数组或扩展数组的内容。因此,如果要使用任意函数作为方法,则需要使用Function.callFunction.applyFunction.bind来设置this

如果你有 Array.prototype.push.apply(thisObject, arrArguments)与...相同 如果thisObject.push(arrArguments[0], arrArguments[1], ..., arrArguments[n])在其原型链中有thisObject,则push。由于perms是一个数组,并且在其自己的原型链中有push,因此可以将其替换为:

perms.push.apply(perms, permutation(arr.slice(0), start + 1, last));

使用apply是因为push获取permutations数组的所有内容作为参数。因此,如果permutations(....)返回[1,2,3],则它将与perms.push(1, 2, 3)同义。你可以通过为每个元素调用apply而不用push来编写它:

for (var e of permutation(arr.slice(0), start + 1, last)) {
    perms.push(e);
}

在ES6中,您只需使用与apply相同的spread syntax,但更容易理解:

perms.push(...permutation(arr.slice(0), start + 1, last))

答案 1 :(得分:0)

扩展它与:

相同

apply()

Array.prototype.push来自Function.prototype.apply()"Array.prototype"是一个函数。

使用空数组而不是编写push()会公开可以调用apply()的{​​{1}}方法并且只是因为"[]"要写的字符少于"Array.prototype" {1}}。

相关问题