Skip to content

Instantly share code, notes, and snippets.

@nodlAndHodl
Last active June 8, 2018 19:32
Show Gist options
  • Save nodlAndHodl/2bdd80767fcc70db2c8b79a0ac580f02 to your computer and use it in GitHub Desktop.
Save nodlAndHodl/2bdd80767fcc70db2c8b79a0ac580f02 to your computer and use it in GitHub Desktop.
ES6 constructors
//Simple implementation of a class using a the ES6 defined constructor function, showing inheritance
//using Vehicle Class to inherit property from
class Vehicle {
constructor(color){
this._color = color;
}
}
//Car inherits from Vehicle class
class Car extends Vehicle{
constructor(color,make, model, year){
//super must be implemented before the child class properties.
super(color)
this._make = make;
this._model = model;
this._year = year;
}
//using a get to return this._ values with get keyword method.
get make(){
return this._make.toUpperCase();
}
get model(){
return this._model
}
get year (){
return this._year;
}
set year(year){
this._year = year;
}
}
car = new Car("Red","Ford", "F-150", 1999);
console.log(car.model);
car.model = "F-250";
console.log(car.model); //do to the set method not being used car.model == 'F-150'
console.log(car.year);
car.year = 2018;
console.log(car.year)//2018, it worked, that old 99 was beat out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment