When an object is created based on the class, the new object is referred to as an instance of the class. New instances are created using the
The code sample shows a class that represents a
Song. A new object called
mySong is created underneath and the
.play() method on the class is called. The result would be the text
Song playing! printed in the console.
JS Class Constructor
constructor method. This is a special method that is called when the object is created (instantiated). Constructor methods are usually used to set initial values for the object.
JS class methods
Properties in JS objects are separated using commas. This is not the case when using the
class syntax. Methods in classes do not have any separators between them.
JS Class extends
extends keyword as part of the class definition.
Child classes have access to all of the instance properties and methods of the parent class. They can add their own properties and methods in addition to those. A child class constructor calls the parent class constructor using the
static keyword defines a static method for a class. Static methods are not called on individual instances of the class, but are called on the class itself. Therefore, they tend to be general (utility) methods.