I have two objects
var dog1 = {
sound: "bark1!",
bark: () => {
console.log(this)
console.log(this.sound);
}
}
and
var dog2 = {
sound: "bark2!",
bark: function() {
console.log(this);
console.log(this.sound);
dog1.bark();
}
}
Now I know that if I call dog1.bark()
I'll have the value of this
to be equal to Window
object since the value of this
for an arrow function is dependent on the lexical scoping.
But if I call dog2.bark()
and from within it when dog1.bark()
is called, shouldn't the value of this
be dog2? Since the lexical scope of the arrow function is the bark function of dog2? Or am I missing something here?
Would appreciate some clarification on this.
I noticed this question got marked duplicate of this but I couldnt understand why calling an object from an old-style function still results in the value of this
being equal to window