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 this
will be what is needed).
Aside from the fact that they are completely different? One returns a closure containing a function call, the other calls the function and returns its return value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.