I wrote a code which I can't understand why it prints undefined
.
I was expecting to see "oops, global" because the call to foo
is not from the object so it must be from the global scope which means that the this
refers to the global this
.
Where am I wrong?
var obj1 = {
a123: 2,
foo: function() {
// console.log(this); obj1 is not in this. why?
console.log(this.a123);
}
};
var a123 = "oops, global";
var f = obj1.foo;
f(); // prints undefined. why? should have printed "oops, global"
I'm running the code in IntelliJ using nodejs
.