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

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -