Getting undefined setting object's property value using this in javascript -
i trying set fullname
property of object person
using this
, got undefined when logging out full name.
var person = { name:'yask', fullname: this.name + ' srivastava' } console.log(person.fullname);
this strange, using this
while using inside function refers object. here looks it's being referred global object.(window maybe..?)
you can use of getter,
var person = { name:'yask', fullname(){ return this.name + ' srivastava' } } console.log(person.fullname);
basically this
in case point context of lexical scope function of object, not object itself.
Comments
Post a Comment