I have a question with regards to the Bind feature in Underscore.js let’s say we have the following object "room":
var person = 'Bob';
$(function () {
var room = {
capacity: 10,
exits: 2,
count: 0,
person: '',
addPerson: function (name) {
this.count += 1;
var nestedFunction = function (nameOfPerson) {
// this is bound to window
this.person = nameOfPerson;
}(name);
}
};
room.addPerson('dave');
});
At the line indicated by my comment, "this" is bound to the window. That is expected behaviour.
Let’s say we want to bind it to the "room" object. Is it possible to do that with Underscore.js' bind method.
Note: I am aware I could handle this with the good old "that = this" routine. But I'm not interested in that.