Accessing this from within an object's inline function
A common way is to assign the this
you want to a local variable.
init: function() {
var _this = this;
this.testObject.submit(function() {
console.log(_this.testVariable); // outputs testVariable
});
}
You could also do this using ES6 arrow functions:
init: function(){
this.testObject.submit( () => {
console.log(this.testVariable);
}
}
Arrow functions capture the this
value of the enclosing context, avoiding the need to assign this
to a new variable, or to use bound functions.