, , : console.log( this.a );
, .
function foo() {
console.log( this.a );
}
this , .
so you set a=2globally and then to objects o.a = 3andp.a=4
So:
the call o.fooreturns 3because it indicateso
the call p.fooreturns 4because it indicatesp
BUT
(p.foo = o.foo)();it will return the call 2because it does not point to any object, so it will take your scope (which is the global scope), and then it will return 2.
if you follow these steps:
p.foo = o.foo
p.foo()
it will return 4 successfully because it points to p.
source
share