Skip to content

Instantly share code, notes, and snippets.

@singerxt
Created February 24, 2015 22:14
Show Gist options
  • Save singerxt/0dfdae92f37292f07d5b to your computer and use it in GitHub Desktop.
Save singerxt/0dfdae92f37292f07d5b to your computer and use it in GitHub Desktop.
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