Javascript - add parameters to a function passed as a parameter
You can always use the bind() function to pass some arguments to your function. It'll create a new function with the first argument - arg1 - equal to the value of 3 in this example:
function one(func){
func(5);
}
function two(arg1, arg2){
console.log(arg1);
console.log(arg2);
}
one(two.bind(null, 3))
You can read more about the bind() function here: MDN - Bind
Some workaround is possible
function one() {
var args = Array.prototype.slice.call(arguments);
var func = args[0];
args.splice(0, 1);
args.push(5);
func.apply(this, args);
}
function two(arg1, arg2) {
console.log(arg1);
console.log(arg2);
}
one(two, 3)
There's a problem with your syntax: function one
is expecting its single argument to be a function. Then, below, when you invoke it, you are not passing the function two
, but whatever two
returns when it's passed a single argument, probably undefined
. I don't know what specifically you're trying to accomplish but I'd recommend a little research into closures.