I am reading some node code written by my ex-colleague. i am not a proficient javascript programmer, but i see lot of code that looks to me like syntactic sugar. for eg:
_.bind(this._work, this),
is not this exactly same as calling
this._work
I am reading some node code written by my ex-colleague. i am not a proficient javascript programmer, but i see lot of code that looks to me like syntactic sugar. for eg:
_.bind(this._work, this),
is not this exactly same as calling
this._work
This creates a copy of the function with this
bound to the correct object. This can be useful when you're passing functions around.
function log(msg) {
document.querySelector('pre').innerText += msg + '\n';
}
var _ = {
bind: function(f, self) {
// Simplified bind implementation
return f.bind(self);
}
};
function runFunc(f) {
f();
}
var obj = {
myName: 'Mike',
printName: function() {
log(this.myName);
}
};
obj.printName(); // "Mike"
runFunc(obj.printName); // undefined
runFunc(_.bind(obj.printName, obj)); // "Mike"
<pre></pre>