【问题标题】:how to change arguments of caller function by Function.prototype.apply()?如何通过 Function.prototype.apply() 更改调用者函数的参数?
【发布时间】:2016-08-30 16:29:54
【问题描述】:
function foo1(a,b){
   console.log(arguments); //["oldValue","oldValue"]

   var newArguments = foo2.apply(this,arguments);
   for (var i=0;i<arguments.length;i++){
      arguments[i] = newArguments[i];   
}
   console.log(arguments); //["newValue","newValue"]
}

function foo2(){
   arguments[0] = "newValue";
   arguments[1] = "newValue";
   console.log(arguments); //["newValue","newValue"]
   return arguments;
}

foo1("oldValue","oldValue");

我想通过外部函数 foo2 更改 foo1 参数值。我通过在 foo2 中返回带有新参数的数组并将 foo1 参数替换为 foo1 中返回的数组来做到这一点。还有其他更优雅的方法吗?

【问题讨论】:

    标签: javascript function arguments apply


    【解决方案1】:

    好的,我找到了解决方案。我刚刚将 apply() 中的第一个参数更改为“参数”。现在它指的是调用函数的参数,通过“this”我可以直接改变它的值。不过还是谢谢支持!

    function foo1(a, b) {
       foo2.apply(arguments,arguments);
       console.log(arguments);  //["newValue","newValue"]
       console.log(a); //"newValue"
    }
    function foo2() {
       this[0] = "newValue";
       this[1] = "newValue";
    };
    foo1("oldValue","oldValue");
    

    【讨论】:

      【解决方案2】:

      您为什么不直接收到arguments

      function foo1() {
        console.log('foo1', arguments); // foo1 { '0': 'oldValue', '1': 'oldValue' }
      
        arguments = foo2.apply(this, arguments); 
      
        console.log('foo1', arguments); // foo1 { '0': 'newValue', '1': 'newValue' }
      
      }
      
      function foo2() {
        arguments[0] = 'newValue';
        arguments[1] = 'newValue';
        console.log('foo2', arguments); // foo2 { '0': 'newValue', '1': 'newValue' }
        return arguments;
      }
      
      foo1('oldValue', 'oldValue');
      


      更新 1

      由于您还想更改ab,我会尝试“再次”调用foo1,如下所示:

      function foo1(a, b) {
        console.log('foo1', arguments);
      
        if (a === 'oldValue') // Detect if `arguments` has been changed or not. 
                              // (You can also use a variable to record the change if you prefer.)
          // If not, change the arguments and call `foo1` using the new arguments
          return foo1.apply(this, foo2.apply(this, arguments));
      
        console.log('foo1 (after changed)', arguments , a, b);
        // Do something you want to do originally in `foo1`
      }
      

      我想您可以创建一个新函数而不是更改foo1 中的参数,因为这对我来说似乎有点棘手?

      【讨论】:

      • 如果我直接接收参数但设置了一些像 foo1(a,b) 这样的属性,那么如果我引用它们,a 和 b 保持不变(而 arguments[0],arguments[1] 返回新值)。
      【解决方案3】:

      https://jsbin.com/jibodu/1/edit?js,console

      如果您要从 foo2 返回两个新参数,只需将参数设置为该返回值:

      arguments = foo2();
      

      完整代码:

      function foo1(a,b){
         console.log(arguments); //["oldValue","oldValue"]
          arguments = foo2();
         var newArguments = foo2.apply(this,arguments);
         for (var i=0;i<arguments.length;i++){
            arguments[i] = newArguments[i];   
      }
         console.log(arguments); //["newValue","newValue"]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-12
        • 1970-01-01
        • 2019-08-04
        • 2019-08-10
        • 2015-09-20
        • 1970-01-01
        • 2021-03-27
        相关资源
        最近更新 更多