Prototypical vs Classical "inheritance" in JavaScript
function Rotorcraft ( rotorBlades ) {
this . rotorBlades = rotorBlades ;
}
Rotorcraft . prototype = {
start ( ) {
console . log ( this , 'Brrrrrrrr...' ) ;
} ,
} ;
function Helicopter ( rotorBlades , mainRotors = 1 ) {
Rotorcraft . call ( this , rotorBlades ) ;
this . mainRotors = mainRotors ;
}
Helicopter . prototype = {
__proto__ : Rotorcraft . prototype ,
autoRotate ( ) {
console . log ( this , 'Eeeek!' ) ;
} ,
} ;
const helo = new Helicopter ( 3 ) ;
function Autogyro ( rotorBlades , rearProp = true ) {
Rotorcraft . call ( this , rotorBlades ) ;
this . rearProp = rearProp ;
}
Autogyro . prototype = {
__proto__ : Rotorcraft . prototype ,
jumpTakeOff ( ) {
console . log ( this , 'Wheeee!' ) ;
} ,
} ;
const gyro = new Autogyro ( 2 ) ;
class Rotorcraft {
constructor ( rotorBlades ) {
this . rotorBlades = rotorBlades ;
}
start ( ) {
console . log ( this , 'Brrrrrrrr...' ) ;
}
}
class Helicopter extends Rotorcraft {
constructor ( rotorBlades , mainRotors = 1 ) {
super ( rotorBlades ) ;
this . mainRotors ;
}
autoRotate ( ) {
console . log ( this , 'Eeeek!' ) ;
}
}
const helo = new Helicopter ( 3 ) ;
class Autogyro extends Rotorcraft {
constructor ( rotorBlades , rearProp = true ) {
super ( rotorBlades ) ;
this . rearProp ;
}
jumpTakeOff ( ) {
console . log ( this , 'Wheeee!' ) ;
}
}
const gyro = new Autogyro ( 2 ) ;