Taken from the JavaScript Ninja, I see:
function bind(context, name) {
return function() {
return context[name].apply(context, arguments);
};
}
What is the difference between the above code and?
function bind(context, name) {
return context[name].apply(context, arguments);
}
I am confused why the extra return is needed?
Thanks
The first returns a function.
The second returns the result of calling
context[name]It allows you to pass a function somewhere (so it can be called later) while maintaining the context (so the value of
thiswill be what is needed).