Skip to content

Instantly share code, notes, and snippets.

@TommyZG
Last active November 12, 2017 10:52
Show Gist options
  • Save TommyZG/a6924bb840acc8c0d1eed3a80ab1f84e to your computer and use it in GitHub Desktop.
Save TommyZG/a6924bb840acc8c0d1eed3a80ab1f84e to your computer and use it in GitHub Desktop.
//declare class
class Surgeon {
//define constructor method - things that will be done on instantiation ('new')
constructor(name, department) { //when 'new-ing' instance of this class, expect 'name' and 'department' arguments
//we use underscore to define properties that aren't supposed to be acccessed directly
this._name = name;
this._department = department;
this._remainingVacationDays = 20;
}
//getter method - name as the property, but without underscore. This will be called with object.name
get name(){
return this._name;
}
get department() {
return this._department;
}
get remainingVacationDays() {
return this._remainingVacationDays;
}
//method in a class, takes an argument
takeVacationDays(daysOff){
this._remainingVacationDays -= daysOff;
}
}
//instantiation of class into an object
const surgeonCurry = new Surgeon('Curry', 'Cardiovascular');
const surgeonDurant = new Surgeon('Durant', 'Orthopedics');
//using object properties and methods
console.log (surgeonCurry.name)
surgeonCurry.takeVacationDays(3);
console.log(surgeonCurry.remainingVacationDays)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment