Why is undefined printed? How can I make this code print global, obj and inner respectively
var scope = 'global';
function log() {
console.log(this.scope)
}
var obj = {
scope: 'obj',
do: function () {
var scope = 'inner';
log()
}
};
obj.do();
The final object ambition is window, window.scope, and what is returned is undefined
this pointing problem
this pointing in different execution environments of JS functions
First of all, what this code prints should be global, not undefined. Then, it is impossible to call inner through this.scope the way you write it. For the rest, just look at the this pointer
var scope = 'global';
function log() {
}
var obj = {
};
obj.do();