+ 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());

3rd Oct 2020, 7:07 AM
Oma Falk
Oma Falk - avatar
2 ответов
+ 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)
3rd Oct 2020, 7:17 AM
Kevin ★
+ 4
Kevin ★ thanks...🤗
3rd Oct 2020, 7:19 AM
Oma Falk
Oma Falk - avatar