Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created September 10, 2017 09:29
Show Gist options
  • Save tkssharma/192f2a9010d6d0f1a262c54cba81fb99 to your computer and use it in GitHub Desktop.
Save tkssharma/192f2a9010d6d0f1a262c54cba81fb99 to your computer and use it in GitHub Desktop.
var person = {
firstname: "Albert",
lastname: "Einstein",
get fullname() {
return this.firstname +" "+this.lastname;
},
set fullname(_name){
var words = _name.toString().split(' ');
this.firstname = words[0];
this.lastname = words[1];
}
};
person.fullname = "Issac Newton";
console.log(person.firstname); //"Issac"
console.log(person.lastname); //"Newton"
console.log(person.fullname); //"Issac Newton"
var person = {
firstname: "Albert",
lastname: "Einstein",
};
Object.defineProperty(person, 'fullname', {
get: function() {
return this.firstname + ' ' + this.lastname;
},
set: function(name) {
var words = name.split(' ');
this.firstname = words[0];
this.lastname = words[1];
}
});
person.fullname = "Issac Newton";
console.log(person.firstname); //"Issac"
console.log(person.lastname); //"Newton"
console.log(person.fullname); //"Issac Newton"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment