如何在javascript中通过apply将参数传递给函数?

如何在javascript中通过apply将参数传递给函数?,javascript,Javascript,我有一些函数,可以将参数从a传递到b 但是apply似乎只需访问2个参数 function a(){ var p1=1; var p2=2; b.apply(this,arguments); } function b(){ //so how to get p1,p2 in this function? console.log(arguments); } a(3,4); 因此,如何在函数a中传递p1p2,并在函数b中获得1,2,3,4?您创建了一个包含所有需要传

我有一些函数,可以将参数从
a
传递到
b

但是
apply
似乎只需访问2个参数

function a(){
  var p1=1;
  var p2=2;
  b.apply(this,arguments);
}


function b(){
    //so how to get p1,p2 in this function?
    console.log(arguments);
}

a(3,4);

因此,如何在函数
a
中传递
p1
p2
,并在函数
b
中获得
1,2,3,4

您创建了一个包含所有需要传递的参数的数组:

var combinedArray = [p1, p2].concat(Array.prototype.slice.call(arguments));
此功能:

  • 创建包含前2个元素的数组,如
    p1
    p2
  • 并将参数数组附加到它
  • 然后您可以将其称为
    b.apply(这是combinedArray)

    附言:


    Array.prototype.slice.call(arguments)
    用于将
    arguments
    对象转换为实际数组。

    创建一个包含所有需要传递的参数的数组:

    var combinedArray = [p1, p2].concat(Array.prototype.slice.call(arguments));
    
    此功能:

  • 创建包含前2个元素的数组,如
    p1
    p2
  • 并将参数数组附加到它
  • 然后您可以将其称为
    b.apply(这是combinedArray)

    附言:


    Array.prototype.slice.call(arguments)
    用于将
    arguments
    对象转换为实际数组。

    为了将变量传递给另一个函数,必须将它们添加到提供给
    apply
    的数组中。但是不能将它们直接添加到
    参数
    变量中,因为它不是真正的数组。因此,首先您必须构建一个新数组,并将其传递给
    apply
    。您可以通过
    var newArray=array.prototype.slice.call(参数)将
    参数转换为数组-这将根据
    参数创建新数组。现在,您可以像处理常规数组一样使用它(
    newArray.push(p1);
    newArray=newArray.concat([p1,p2]);
    等等)。然后将该数组传递给
    apply
    ,而不是
    参数

    因此,您的代码将以这种方式更改:

    function a(){
      var p1=1;
      var p2=2;
      var argumentsArray = Array.prototype.slice.call(arguments);
      b.apply(this, argumentsArray.concat([p1, p2]));
    }
    
    
    function b(){
        console.log(arguments);
    }
    
    a(3,4); // output: [3, 4, 1, 2];
    

    如果需要预先添加
    p1
    p2
    ,可以使用将变量传递给另一个函数,您必须将它们添加到提供给
    应用的数组中。但是不能将它们直接添加到
    参数
    变量中,因为它不是真正的数组。因此,首先您必须构建一个新数组,并将其传递给
    apply
    。您可以通过
    var newArray=array.prototype.slice.call(参数)将
    参数转换为数组-这将根据
    参数创建新数组。现在,您可以像处理常规数组一样使用它(
    newArray.push(p1);
    newArray=newArray.concat([p1,p2]);
    等等)。然后将该数组传递给
    apply
    ,而不是
    参数

    因此,您的代码将以这种方式更改:

    function a(){
      var p1=1;
      var p2=2;
      var argumentsArray = Array.prototype.slice.call(arguments);
      b.apply(this, argumentsArray.concat([p1, p2]));
    }
    
    
    function b(){
        console.log(arguments);
    }
    
    a(3,4); // output: [3, 4, 1, 2];
    
    如果您需要预编
    p1
    p2
    ,可以使用尝试此代码

    void temp() { 
        var animals = ["cat", "dog", "horse", "cow"];
        animals.forEach(function (eachName, index) {
         console.log(index + 1 + ". " + eachName); 
        });
    }
    
    试试这个代码

    void temp() { 
        var animals = ["cat", "dog", "horse", "cow"];
        animals.forEach(function (eachName, index) {
         console.log(index + 1 + ". " + eachName); 
        });
    }
    

    请不要建议像那样对
    参数进行切片。看。@ralh如果它被调用一次-那没关系。如果它被调用了百万次,OP就会意识到这一点。我不知道如果OP被多次调用,它是如何意识到去优化的。也许他最终会发现,但你仍然给出了不完美的建议。@ralh我应该推荐什么?对于不是通用高性能库的东西来说,这永远不会是问题。请不要建议像这样切片
    参数。看。@ralh如果它被调用一次-那没关系。如果它被调用了百万次,OP就会意识到这一点。我不知道如果OP被多次调用,它是如何意识到去优化的。也许他最终会发现,但你仍然给出了不完美的建议。@ralh我应该推荐什么?对于不是通用高性能库的东西来说,这永远不会是问题。这如何回答这个问题?这如何回答这个问题?