In the following learners piece of code, call to foo() below was expected to return "bar1" based on the following understanding that in non-strict mode, the this keyword binds to global based on the default binding rule and undefined in strict mode.
function foo(){
console.log(this.bar);
}
var bar = 'bar1';
var o1 = {bar: 'bar2', foo: foo};
var o2 = {bar: 'bar3', foo: foo};
foo(); // expect 'bar1' - default binding
o1.foo(); // expect 'bar2' - implicit binding
o2.foo(); // expect 'bar3' - implicit binding
However the on calling foo() I'm getting undefined in console. o1.foo() and o2.foo() behaves as expected based on implicit binding. It seems to me that the binding is working as expected, but the bar variable is never stuck on to global.
I ran the script in node 6
> node script.js
undefined
bar2
bar3
Note:- in browser it gives the expected output.