admin管理员组

文章数量:1406950

the function takes 3 parameters like

function p(x,y,z){
 console.log(arguments);
}

so when we call it like p(12,21,32)

a fourth argument should pass as say 56

so effectively the call should be p(12,21,32,56)

How to do this?

Condition We cannot change the function definition. I need to partially bind the fourth argument as 56 something like

p=p.bind(this,'','','',56); or use lodash

and then call p later like

p(12,21,32);

such that 56 should pass by default

the function takes 3 parameters like

function p(x,y,z){
 console.log(arguments);
}

so when we call it like p(12,21,32)

a fourth argument should pass as say 56

so effectively the call should be p(12,21,32,56)

How to do this?

Condition We cannot change the function definition. I need to partially bind the fourth argument as 56 something like

p=p.bind(this,'','','',56); or use lodash

and then call p later like

p(12,21,32);

such that 56 should pass by default

Share Improve this question edited Jun 23, 2015 at 2:06 Shishir Arora asked Jun 23, 2015 at 2:03 Shishir AroraShishir Arora 5,9534 gold badges33 silver badges36 bronze badges 1
  • 1 you can always define function with 4 arguments, and whether or not to pass the forth argument when call the method is up to you. – Surely Commented Jun 23, 2015 at 2:10
Add a ment  | 

4 Answers 4

Reset to default 6

You can use _.partialRight() to create a new function that appends arguments to the end of the original function:

function p(a, b, c)
{
  alert([].join.call(arguments, ','));
}

p = _.partialRight(p, 56);
p(1,2,3); // 1,2,3,56
<script src="https://raw.githubusercontent./lodash/lodash/3.9.3/lodash.js"></script>

To exactly specify the position of the extra argument(s) you can use placeholders:

p = _.partialRight(p, _, _, _, _, _, _, 56); // add 56 as 7th arg
p(1,2,3); // 1,2,3,,,,56
p = (function() {
    var old_p = p;
    return function(a, b, c) {
        return old_p(a, b, c, 56);
    };
})();

We remember the old version of p under the name old_p so we can invoke it even after we've redefined p. We do this inside the IIFE so that old_p does not pollute the global scope. Then, we return a function (which is assigned to p) which returns the result of calling old_p with the extra argument.

We can make this more general, to create "bound" functions which add extra arguments to any function call. Below I use ES6 syntax, especially the spread ... operator. However, you can acplish the same thing by manipulating the arguments object and using apply:

function bind_with_arguments_at_end(f, ...extra_args) {
    return function(...args) {
        return f(...args, ...extra_args);
    }
}

Where the function involved is a method on an object, it makes sense to "pass through" this, so the new function can be called as this.bound_func and things continue to work. Do do this, we can use call:

function bind_with_arguments_at_end(f, ...extra_args) {
    return function(...args) {
        return f.call(this, ...args, ...extra_args);
               ^^^^^^^^^^^
    }
}

You can create a new function which uses apply to redirect its arguments to the original one, but using Object.assign to overwrite some of them:

function fixArguments(f, args) {
  return function() {
    return f.apply(this, Object.assign([].slice.call(arguments), args));
  };
}
p = fixArguments(p, {3: 56}); // Overwrite the 4th argument with 56

function fixArguments(f, args) {
  return function() {
    return f.apply(this, Object.assign([].slice.call(arguments), args));
  };
}
function p(x,y,z){
  console.log(arguments);
}
p = fixArguments(p, {3: 56});
p(12,21,32);

Make a copy of the original and override the name, and call the original with the new arguments.

function p(a,b,c,d) {
   console.log(arguments);
}

(function (){
    var org_p = p;  //copy original
    p = function() {  //override p
        var args = [].slice.call( arguments );  //turn arguments in to array
        args.push(56);  //add the 4th argument
        return org_p.apply( this, args );  //call the original with the updated arguments.
    }
}());

p(1,2,3);

本文标签: lodashjavascript always pass the nth argument in a function as fixed value by defaultStack Overflow