Why is this undefined in example one, but defined in example two? I thought the arrow notation would allow the binding to be on higher scope, in this case the object literal.
Example 1:
var runApp = {
init: () => {
console.log(this); //this is an empty object.
this.run() //will crash here, run not a function of this.
},
run: () => {
console.log("It's running!");
}
};
// Now we call init
runApp.init();
Example 2:
var runApp = {
init: function(){
console.log(this);
this.run()
},
run: function() {
console.log("It's running!");
}
};
// Now we call init
runApp.init();