ES6 Classes part 1
Classes in ES6
In this lesson we'll explain how to create a class that can be used to create multiple objects of the same structure.
A class uses the keyword class and contains a constructor method for initializing.
For example:class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
A declared class can then be used to create multiple objects using the keyword new.
For example:
Class Declarations are not hoisted while Function Declarations are. If you try to access your class before declaring it, ReferenceError will be returned.
You can also define a class with a class expression, where the class can be named or unnamed.
A named class looks like:
In the unnamed class expression, a variable is simply assigned the class definition:
The constructor is a special methodwhich is used for creating and initializing an object created with a class.
There can be only one constructor in each class.
Comments
Post a Comment