explanation about prototype.js function binding code
- by resopollution
From: http://ejohn.org/apps/learn/#2
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
Can anyone tell me why the second return is necessary (before fn.apply)?
Also, can anyone explain why args.concat is necessary? Why wouldn't it be re-written as:
fn.apply(object, args)
instead of
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));