Because you change the scope when you call Object.keys(object).map this will change too so you can't access the original value by using it. Aliasing it to that allows you still to access the original value of this. Your code will loook like this.
var AppView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
this.$el.html("Hello World");
this.one();
},
one: function(){
this.two();
},
two: function(){
var that = this;
var object = { "labname4": "423",
"Path": "4",
"X": "4"};
console.log('two');
Object.keys(object).map(function(objectKey, index) {
var value = object[objectKey];
that.three();
});
},
three: function(){
console.log('three');
},
});
var appView = new AppView();
console.log("done");
Edit 1 var that = this vs bind
To me, it seems that .bind definitely has it's place. For instance when you have a function in a variable and you want to attach it to a particular this.
That said, in the case of nested anonymous functions I think that using var that = this; is straightforward and requires less mental accounting vs. using .bind. For example, say we have two nested functions:
function foo() {
return (function() {
doSomething((function() {
return this.name; // what is `this` again?
}).bind(this));
}).bind(this);
}
vs
function foo() {
var that = this;
return (function() {
doSomething((function() {
return that.name; // `that` is easy to know because its defined
})
})
}
Of course, because this is a matter of style the issue of "mental accounting" will differ from person to person. But to me, given the semtanics of Javascript's this, I think that that = this is easy to comprehend when you have nested callbacks.