I'm just playing around and trying to figure out how the this object works and how do I pass scopes context with the bind function.
So far I got to this point (check code below), and found out that I can use bind with an object function in order to pass the scope of myObject to the object function run.
first question:
why do I need to use this.first() instead of first()?
Because otherwise its still using second's context.
myObject = {
first: function(){ console.log("myObject's first()")},
second: function(){
function first(){ console.log("second's first()")};
let run = function(){ this.first()}.bind(this);
run();
},
}
myObject.second()
Next, I wanted to take it to the next level and I nested another function within second (check code). Second question:
From the code below i get the error Error: this.first is not a function, and whether i invoke .bind(this) or not it gives the same error, any clue why does this happen?
myObject = {
first: function(){ console.log("myObject's first()")},
second: function(){
function first(){ console.log("second's first()")};
function secondx2(){
function first(){ console.log("secondx2's first()")};
let run = function(){ this.first()}
run();
}
secondx2();
},
}
myObject.second()