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.
assume we have drawRect(x, y, x2, y2) I want to write a function named 'partial' so that
(partial(drawRect, 0, 0))(x2, y2);
be equal to
drawRect(0, 0, x2, y2);
please note that I want to function partial be flexible so it takes any function and any number of arguments.
so far I've written this:
function temp(a, b, c, d){
document.write(a + b + c + d);
}
function partial(func){
var arr = new Array();
for (var i = 0; i<arguments.length; i++){
arr.push(arguments[i+1]);
}
var tf = function(){f.apply(this, arr.concat(func.arguments))}
console.log(2);
return tf;
}
var ff = partial(temp, 44, 55);
ff(20, 30);
but it writes a 'Nan' on document. I need to access to function 'ff' arguments when calling but I've no idea how to do it. Can you guys please help me solving this?