Last active
May 31, 2019 21:17
-
-
Save krfong916/7fd7767f6dbde8cc1e5e32fb921e9047 to your computer and use it in GitHub Desktop.
es6 classes, prototypical inheritance, static keyword
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Food { | |
static isHealthy() { | |
return true | |
} | |
static isEdible() { // references it's own class | |
return this.isHealthy() | |
} | |
} | |
console.log(Food.isEdible()) | |
class Vehicle { | |
isLegal() { | |
return true | |
} | |
} | |
class Car extends Vehicle { | |
canUse() { | |
return this.isLegal() | |
} | |
} | |
console.log(new Car.canUse()); // returns true | |
console.log(Vehicle.prototype.isLegal()) // invoke property on prototype | |
class Person { | |
constructor(id) { | |
// shadowing of the id() method | |
// with a property value on the instance | |
this.id = id; | |
} | |
id () { | |
} | |
} | |
const abigail = new Person('cc'); | |
abigail.id(); // shadowing of the id() method with a property value on the instance we talked about above |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ES6 Classes
Overview
class
keyword operates on the[[prototype]]
(delegation) mechanism; therefore, is just syntactic sugar over the function. All objects declared with aclass
keyword will have a prototype object.extends
- the prototype object, and not the class itself, is prototype linked to another class’s prototype object.Pseudo-Inheritance, still prototype-linked
In our example,
Car
is prototype linked toVehicle's
prototype using theextends
keyword.extends
gives the appearance of a classical parent-child relationship, however, instead of properties being copied from one class to another, methods and properties are actually created on the prototype object of that class.To demonstrate, we can invoke
isLegal()
on the prototype object ->console.log(Vehicle.prototype.isLegal()
.Classes, in further detail
To explain ES6 class behavior further, let's discuss why the code
console.log(new Car.canUse())
returns true. This is because thethis
keyword is referencing the newly created object we get from using thenew
keyword on ourCar
class. This newly created object is prototype linked to theVehicle's
prototype object through extends.Again, if looking to access a method (property) on the class, we must note that the method doesn’t exist on the class, the method is written to the prototype object of the class.
Static keyword
To assign the method (property) to the class - we use the static keyword.
The static keyword will assign it to the class instead of the prototype of the class
Static properties can be called in its own class, and we can use the ’this’ keyword, because the static property is referencing it’s own class
Shadowing
see above
Gotchas
For an alternative: see https://github.com/getify/You-Dont-Know-JS/blob/f0d591b6502c080b92e18fc470432af8144db610/this%20%26%20object%20prototypes/ch6.md#nicer-syntax