What's the most efficient/the best practice for passing a Javascript function with parameters without executing it, and why? Here is the choice I know :
Make a anonymous method
jQuery("#target").click(function() {
onTargetClick("myCoolParam");
});
function onTargetClick(coolParam) {
// some epic code...
}
Use partial function application (See: Partial function application)
jQuery("#target").click(partial(onTargetClick, "myCoolParam"));
function onTargetClick(coolParam) {
// some epic code...
}
function partial(func /*, 0..n args */) {
var args = Array.prototype.slice.call(arguments).splice(1);
return function() {
var allArguments = args.concat(Array.prototype.slice.call(arguments));
return func.apply(this, allArguments);
};
}
EDIT for the partial function
function partial(func /*, 0..n args */) {
var args = Array.prototype.slice.call(arguments, 1);
return function() {
var allArguments = args.concat(Array.prototype.slice.call(arguments));
return func.apply(this, allArguments);
};
}
Thank you for your help.