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.
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.
Classes can have a
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.
Properties in 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.
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