DevLeague Coding Bootcamp
DevLeague is a Full Stack Coding Bootcamp
Classical Inheritence
Classes define the properties and methods that it's instances have.
Classes are not to be treated like objects, instead think of them like blueprints for how objects are structured and behave.
Are the instances of classes.
All objects constructed from a class share the same properties and methods, while individual object property values may differ.
defining
have a name
class Rocket {
}
can define a constructor method
class Rocket {
constructor(model, destination) {
}
}
can define properties
fuel is a public properties
_launchCount, _model and _destination are private
class Rocket {
constructor(model, destination) {
this._model = model;
this._destination = destination;
}
}
can define accessor methods
(getters and setters) to access private properties
class Rocket {
constructor(model, destination) {
this._model = model;
this._destination = destination;
}
get model() {
return `Rocket Model: ${this._model}`;
}
set model(model) {
if (typeof(model) === 'string') {
this._model = model;
}
else {
throw new TypeError('Rocket.model must be a string');
}
}
}
can define accessor methods
(getters and setters) to access private properties
class Rocket {
constructor(model, destination) {
this._model = model;
this._destination = destination;
this._launchCount = 0;
}
get launchCount() {
return this._launchCount;
}
set launchCount(launchCount) {
if(typeof(launchCount) === 'number') {
this._launchCount = launchCount;
}
else{
throw new TypeError('Rocket.launchCount must be a number');
}
}
}
can define methods
class Rocket {
constructor(model, destination) {
this._model = model;
this._destination = destination;
this._launchCount = 0;
}
launch() {
this._launchCount++;
console.log('to the moon');
}
}
class Vehicle {
constructor(destination) {
this._destination = destination;
}
get destination() {
return this._destination;
}
set destination(destination) {
if(typeof(destination) === 'string') {
this._destination = destination;
}
else{
throw new TypeError('Vehicle.destination must be a string');
}
}
}
class Rocket extends Vehicle {
constructor(model, destination) {
super(destination);
this._model = model;
}
}
// Vehicle is not defined
import Vehicle from './Vehicle';
class Rocket extends Vehicle {
constructor(model, destination) {
super(destination);
this._model = model;
}
}
Instances of Classes
use the new keyword to create an instance
the constructor method is invoked on creation
constructor arguments can be passed in
new Rocket('Raccoon', 'The Moon');
Classes cannot be invoked like normal functions
Rocket('Raccoon', 'The Moon');
// Cannot call a class as a function
assign the instance to a variable
use dot notation or bracket notation to access properties
const raccoon = new Rocket('Raccoon', 'The Moon');
raccoon.fuel;
raccoon['fuel'];
access properties through getter and setter methods as if they normal are properties
const raccoon = new Rocket('Raccoon', 'The Moon');
raccoon.model; // 'Rocket Type: Raccoon'
raccoon.model = 'Lunar Raccoon';
raccoon['model']; // 'Rocket Type: Lunar Raccoon'
raccoon['model'] = 404; // Rocket.model must be a string
assigning a new property to an object will implicitly invoke it's set accessor method
invoke instance methods normally
use dot notation or bracket notation
const raccoon = new Rocket('Raccoon', 'The Moon');
raccoon.launch(); // 'to the moon'
raccoon['launch'](); // 'to the moon'
from the superclass
const raccoon = new Rocket('Raccoon','The Moon');
raccoon.destination; // The Moon
raccoon.destination = 404; // Vehicle.destination must be a string
use instanceof
const raccoon = new Rocket('Raccoon', 'The Moon');
raccoon instanceof (Rocket) // true
raccoon instanceof Rocket // true
raccoon instanceof (Vehicle) // true
raccoon instanceof Vehicle // true
By DevLeague Coding Bootcamp
ES6 OOP