DEFINE A CLASS AND ITS CONSTRUCTOR
A constructor function normally receives parameters as input. The keyword this makes sure that the objects created by the constructor function have a copy of those properties, properties having as values the values passed in as input. Let us see with a code example the differences between a constructor function and a class.
Creating an object from a constructor function or creating an instance from a class with the same properties gives the same results.
The difference is that the properties of a class must be initialized in a special method called constructor.
METHODS
Within a class, we cannot define variables with let, var or const as we do with constructor functions. Methods defined in a class are placed in the prototype object of the class.
PUBLIC AND PRIVATE PROPERTIES
avatars defined in the code below is a public property accessible through the objects. From ES2019 we can define properties private accessible only internally to the class.
GETTER AND SETTER METHODS
A getter method returns a value, a setter method sets the value of a property. Class objects interface with getter and setter methods as if they were properties.
STATIC METHODS AND STATIC PROPERTIES
In some cases we may want to access a method of a class without instantiating it. We can do this with static methods. A static method cannot access non-static methods while the vice versa applies.
EXTEND A CLASS
Using the keyword extends we can inherit properties and methods from a class called superclass into another usually called subclass or derived class.
RELATIONSHIPS BETWEEN SUPERCLASSES AND SUBCLASSES
Using instanceof we can understand the hierarchy between superclasses and subclasses and their prototypes.
Leave A Comment