- Sugar over functions and the prototype chain
- While ES6 Classes arae syntactic sugar, they also also have their own things that only exist with classes. -Classes are not hoisted, unlike functions which are. -Has many keywords specific to ‘class’, but it is still just sugar.
-
Subclasses custom classes and built-in objects.
-
Equivalent of using
Object.setPrototypeOf
with two functions before new keyword. Example:
class Square extends Rectangle {}
Object.getPrototypeOf(Square) // class Rectangle {}
- Special method on a class. It is what is called when you new up a class.
- One
constructor
per class.
- Used to set the property to the class itself.
- Will not go onto the
.prototype
property - Should not be called on instances of class
Super is a way of accessing methods whether static or constructor.. super gives us access to anything on the parent class. Your child class has to call ‘super’ before it uses ‘this.`
The new
keyword goes through classes on extends and looks at constructors. To complete the chain, super
is required in children constructors. Must call super on child classes.
-
Things have been built around ES6 classes to make classes work like how they would in classical languages.
-
When we create classes in react we are extending react.component. It was a child class of another class and it had a constructor, which means we need to use Super.