Help please I don't understand this code why it print 42 then 43 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help please I don't understand this code why it print 42 then 43

var data =41; var o ={data : 42}; o.print =function(){ console.log(this.data); var print2 = function(){ console.log(this.data); } print2() ; }; var data =43 ; o.print()

27th Jan 2019, 12:28 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
5 Answers
+ 1
In the print2 function the keyword "this" refers to the window object. In print2 function this.data is equivalent to window.data.
27th Jan 2019, 2:22 PM
Игорь Яковенко
Игорь Яковенко - avatar
27th Jan 2019, 2:28 PM
Игорь Яковенко
Игорь Яковенко - avatar
0
The keyword "this" refers to the current object. In the o.print function, this.data is equivalent to o.data.
27th Jan 2019, 1:26 PM
Игорь Яковенко
Игорь Яковенко - avatar
0
But why we get 42 43 as output
27th Jan 2019, 1:37 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
0
Thank you so much 👍👍👍
27th Jan 2019, 2:49 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar