As far as I understand, "execution context" is determined by where a function is called — not where it is declared.
So in the example below I would expect this.name
to evaluate to undefined. Thus I'm not sure how console.log()
has access to the this
keyword inside of the "franchise" object.
let franchise = {
name: 'How to Train Your Dragon',
allMovies: function() {
console.log(this.name)
},
};
franchise.allMovies() // How to Train Your Dragon
The reason I would expect it to evaluate to "undefined" is the same as in this example:
let franchise = {
name: 'How to Train Your Dragon',
allMovies: function() {
function print() {
return this.name;
}
return print(); //undefined
}
};