Introduction to Classes
Video
JavaScript Notes
JavaScript
/************************************
JAVASCRIPT STILL USES PROTOTYPES!!!
class
extends
constructor
super
get
set
static
***********************************/
class Vehicle{
// each class will usually have a constructor method
constructor(_wheels){
this.numWheels = _wheels;
}
get wheels( ){ //gets, creates a method called "wheels"
return this.numWheels;
}
set wheels(_wheels){ //sets creats a method called "wheels"
this.numWheels = _wheels;
}
static accelerate( ){
console.log('go faster');
}
static decelerate( ){
console.log('go slower');
}
}
let car = new Vehicle(4);
console.log(car.wheels);
Vehicle.accelerate( ); // Vehicle, here, is like the prototype for Car!
class Car extends Vehicle{
constructor(_wheels, _make, _model, _year){
super(_wheels); // super calls the constructor of the Vehcile class
this.make = _make;
this.model = _model;
this.year = _year;
}
info( ){
console.log('The', this.make, this.model, 'was made in', this.year, 'and has', this.wheels, 'wheels');
}
}
let ferrari = new Car(4, 'Ferrari', 'Testarossa', 1985);
ferrari.info( );
Car.accelerate( );
Feedback
Submit and view feedback