Skip to content

Instantly share code, notes, and snippets.

@shoupn
Last active Jun 8, 2018
Embed
What would you like to do?
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