ES6 introduced classes.
A class is a type of function, but instead of using the keyword function
to initiate it, we use the keyword class
, and the properties are assigned inside a constructor()
method.
A simple class constructor:
class Car {
constructor(name) {
this.brand = name;
}
}
Notice the case of the class name. We have begun the name, "Car", with an uppercase character. This is a standard naming convention for classes.
Class Inheritance
To create a class inheritance, use the extends
keyword.
A class created with a class inheritance inherits all the methods from another class:
The super()
method refers to the parent class.
By calling the super()
method in the constructor method, we call the parent's constructor method and gets access to the parent's properties and methods.
Arrow Functions
Arrow functions allow us to write shorter function syntax:
Before:
hello = function() {
return "Hello World!";
}
With Arrow Function:
hello = () => {
return "Hello World!";
}
return
keyword:Arrow Functions Return Value by Default:
hello = () => "Hello World!";
hello = () => "Hello World!";
0 Comments