Javascript add extra argument

后端 未结 11 1009
暖寄归人
暖寄归人 2021-02-06 22:02

Lets take a look at this code:

var mainFunction = function() {
  altFunction.apply(null, arguments);
}

The arguments that are passed to \"mainF

相关标签:
11条回答
  • 2021-02-06 22:07

    The arguments object isn't an array; it's like an array, but it's different. You can turn it into an array however:

    var mainArguments = [].slice.call(arguments, 0);
    

    Then you can push another value onto the end:

    mainArguments.push("whatever");
    
    0 讨论(0)
  • 2021-02-06 22:10

    var myABC = '12321'; someFunction(result, error, myCallback.bind(this, myABC));

    Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

    0 讨论(0)
  • 2021-02-06 22:13

    Use Array.prototype.push

    [].push.call(arguments, "new value");
    

    There's no need to shallow clone the arguments object because it and its .length are mutable.

    (function() {
        console.log(arguments[arguments.length - 1]); // foo
    
        [].push.call(arguments, "bar");
    
        console.log(arguments[arguments.length - 1]); // bar
    })("foo");
    

    From ECMAScript 5, 10.6 Arguments Object

    1. Call the [[DefineOwnProperty]] internal method on obj passing "length", the Property Descriptor {[[Value]]: len, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}, and false as arguments.

    So you can see that .length is writeable, so it will update with Array methods.

    0 讨论(0)
  • 2021-02-06 22:16
    //
    //    var
    //        altFn  = function () {}, 
    //        mainFn = prefilled( altFn  /* ...params */ );
    //
    //    mainFn(  /* ...params */  );
    //  
    //        
    function prefilled ( fn /* ...params */ ) { 
         return ( function ( args1 ) { 
    
              var orfn = this;
    
              return function () { 
    
                  return orfn.apply( this, args1.concat( cslc( arguments ) ) );
    
              };
    
         } ).call( fn, cslc( arguments, 1 ) );
    }
    
    // helper fn
    function cslc( args, i, j ) { 
       return Array.prototype.slice.call( args, i, j );
    }
    
    
    // example
    
    var
        f1 = function () { console.log( cslc( arguments ) ); }, 
        F1 = prefilled( f1, 98, 99, 100 );
    
    F1( 'a', 'b', 'c' );
    
    //
    //   logs: [98, 99, 100, "a", "b", "c"]
    //
    //
    
    0 讨论(0)
  • 2021-02-06 22:16

    In this case it could be more comfortable to use call() instead of apply():

    function first(parameter1, parameter2) {
    
      var parameter3 = "123";
    
      secondFunction.call(
        this,
        parameter1,
        parameter2,
        parameter3);
    },
    
    0 讨论(0)
  • 2021-02-06 22:17

    arguments is not a pure array. You need to make a normal array out of it:

    var mainArguments = Array.prototype.slice.call(arguments);
    mainArguments.push("extra data");
    
    0 讨论(0)
提交回复
热议问题