Skip to content

Instantly share code, notes, and snippets.

@chiro-hiro
Created March 9, 2020 03:26
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 chiro-hiro/1ebc61c54af9573e6429d858e8f4e062 to your computer and use it in GitHub Desktop.
Save chiro-hiro/1ebc61c54af9573e6429d858e8f4e062 to your computer and use it in GitHub Desktop.
class Human {
constructor(name) {
this.name = name;
this.race = 'Terran';
}
set name(value) {
if (typeof value !== 'string') {
throw new TypeError('Name value was not a string');
}
this._name = value
.split(' ')
.map(function (item) {
return item[0].toUpperCase() + item.substring(1);
})
.join(' ');
}
get name() {
throw new Error('This value can not be read by this way');
}
getName() {
return this._name;
}
}
class Man extends Human {
constructor(name) {
super(name);
this.gender = 'Male';
}
}
const instanceHuman = new Human('chiro hiro');
console.log('My name is:', instanceHuman.getName(), 'Race:', instanceHuman.race);
const instanceMan = new Man('Some body we used to know');
console.log('My name is:', instanceMan.getName(), 'Race:', instanceMan.race, 'Gender:', instanceMan.gender);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment