Class declaration
1 | class Rectangle { |
A class expression
is another way to define a class. Class expressions can be named or unnamed.
1 | // unnamed |
Understand difference between [class] and function constructor
An important difference between function declarations and class declarations is that function declarations are hoisted and class declarations are not. You first need to declare your class and then access it, otherwise code like the following will throw a ReferenceError:
1 | const p = new Rectangle(); // ReferenceError |
Understand difference between [class] and function constructor
The constructor
method is a special method for creating and initializing an object created with a class. There can only be one special method with the name “constructor” in a class. A SyntaxError
will be thrown if the class contains more than one occurrence of a constructor
method.
A constructor can use the super
keyword to call the constructor of the super class.
Understand difference between method and [class] method
Prototype methods
1 | class Rectangle { |
Static methods
1 | class Point { |
Inheritance
The extends
keyword is used in class declarations or class expressions to create a class as a child of another class.
1 | class Animal { |
Getter/setter
The get
syntax binds an object property to a function that will be called when that property is looked up.
1 | var obj = { |
The set
syntax binds an object property to a function to be called when there is an attempt to set that property.
1 | var language = { |