Skip to content Skip to sidebar Skip to footer

Are There Any Drawbacks In Doing This: Adding A New Function In Prototype That Returns The Same Function With Arguments Applied

lot of times when passing function by reference in javascript usually to events i have to write: something.onclick = function(){ callback(3); }; but i rather always want to d

Solution 1:

You can also avoid affecting Function prototype. The approach would be like this:

function callbackWrapper() {
    var args = Array.prototype.slice.call(arguments, 0), fn = args.shift();
    return function() { return fn.apply(null, Array.prototype.concat.apply(args, arguments)); };
}

var adderTo1 = callbackWrapper(function(a, b) { return a + b; }, 1);
alert(adderTo1(2));

Post a Comment for "Are There Any Drawbacks In Doing This: Adding A New Function In Prototype That Returns The Same Function With Arguments Applied"