+ 5
I don't get: output is 3 1 but why?
What is the output of this code? 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());
2 Answers
+ 8
var go = one.two.three the this context is lost when referencing to the method. Then when go is called this refers to the global context instead of the original object.
EDIT: To keep it simple. In Javascript this refers to the object that calls the method.
go() <- There's not dot here.
one.two.three() you get it.
Try:
var go = one.two.three.bind(one.two)
+ 4
Kevin â
thanks...đ€