+ 2

Can anyone explain?

var x = 3; var one = { x: 2, two: { x: 1, three: function() { return this.x; } } } var go = one.two.three; console.log(go(), one.two.three());

25th Oct 2020, 2:00 PM
Manish Kumar
2 Risposte
+ 3
`var go` Use of 'var' here indicates that variable <go> is stored as an attribute of 'window' object. For this reason when <go> is invoked, <x> refers the one in 'window' object context. That is the one registered by `var x = 3;` But when the 'three' method is invoked directly, <x> refers to a member of object 'two'. I guess that's about it cmiiw 👌
25th Oct 2020, 2:40 PM
Ipang
+ 1
Ipang thank you ;)
25th Oct 2020, 8:04 PM
Manish Kumar