Skip to content

Instantly share code, notes, and snippets.

Created January 27, 2015 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3aa1b2597aefd8a51a47 to your computer and use it in GitHub Desktop.
Save anonymous/3aa1b2597aefd8a51a47 to your computer and use it in GitHub Desktop.
function NamedOne(first, last) {
this.firstName = first;
this.lastName = last;
Object.defineProperty(this, 'fullName',{
get: function () {return this.firstName + ' ' + this.lastName;},
set: function (value) {
var splittedString = value.split(' ');
if(splittedString.length === 2) {
this.firstName = splittedString[0];
this.lastName = splittedString[1];
}
}
});
}
x = new NamedOne('John', 'Cat');
x.firstName; //John
x.fullName; //Juan Cat
x.firstName = 'Micaela'; //Micaela
x.fullName; //Micaela Cat
x.fullName = 'Amanda Dog';
x.firstName; // Amanda
x.lastName; // Dog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment