0

unexpected behaviour of 'this' keyword

var name="globalName" var age=12 var person={name:"abc", age:6, callMe: function () { return (`Name is:${this.name} Age is:${this.age}`) } } console.log(person.callMe()) // expected & actual output =>Name is:abc Age is:6 //calling a function var p1=person.callMe() console.log(p1) // expected & actual output =>Name is:abc Age is:6 //assign refrence of function var p2=person.callMe console.log(p2()) // Name is:undefined Age is:undefined

2nd Jun 2019, 4:48 AM
waqar ahmad
waqar ahmad - avatar
1 Resposta
2nd Jun 2019, 4:57 AM
Yathin Babu
Yathin Babu - avatar