Accessing Outer Caller Function Arguments
I want to write a function that takes a function and some other objects as arguments and makes these arguments as default arguments for the function and returns a new function. ass
Solution 1:
There seemed to be a lot of unncessary testing code in there, which I've cleaned up. To my understanding, the code below and this demo do what you required.
function temp(a, b, c, d){
console.log(a + b + c + d);
}
function partial(f){
var defaultArgs = Array.prototype.slice.call(arguments, 1);
var that = this;
var tf = function(){
f.apply(that, defaultArgs.concat(Array.prototype.slice.call(arguments)));
}
return tf;
}
var ff = partial(temp, 44, 55);
ff(20, 30);
// console output: 149
Solution 2:
You can do it with .bind()
function partial(f, x, y) {
return f.bind(this, x, y);
}
Post a Comment for "Accessing Outer Caller Function Arguments"