Created
February 24, 2015 22:14
-
-
Save singerxt/0dfdae92f37292f07d5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Person = function (first, last) { | |
this.firstName = first; | |
this.lastName = last; | |
}; | |
Object.defineProperty(Person, 'species', { | |
writable: false, | |
value: 'human' | |
}); | |
Object.defineProperty(Person, 'fullName', { | |
get: function () { | |
return this.firstName + ' ' + this.lastName; | |
}, | |
set: function (value) { | |
var splitString = value.trim().split(' '); | |
if(splitString.length === 2) { | |
this.firstName = splitString[0]; | |
this.lastName = splitString[1]; | |
} | |
} | |
}); | |
var woman = new Person('Kate', 'Khowalski'); | |
woman.firstName; // 'Kate' | |
woman.lastName; // 'Khowalski' | |
woman.fullName; //'Kate Khowalski | |
woman.species; // human | |
/* | |
* Change name | |
*/ | |
woman.firstName = 'Yulia'; | |
woman.firstName; // 'Yulia' | |
woman.lastName; // 'Khowalski' | |
woman.fullName; // 'Yulia Khowalski' | |
woman.species = 'fish'; | |
woman.species; // human - No, you can't change this properity. | |
/* | |
* Change fullName | |
*/ | |
woman.fullName = 'Joana Stevens'; | |
woman.firstName; //Joana | |
woman.lastName; //Stevens |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment