JS Tutorials
JS Objects
JS Functions
JS Classes
JS Async
ECMAScript 2015, also known as ES6, introduced JavaScript Classes.
JavaScript Classes are templates for JavaScript Objects.
Use the keyword class
to create a class.
Always add a method named constructor()
:
The example above creates a class named "Car".
The class has two initial properties: "name" and "year".
A JavaScript class is not an object.
It is a template for JavaScript objects.
When you have a class, you can use the class to create objects:
The example above uses the Car class to create two Car objects.
The constructor method is called automatically when a new object is created.
The constructor method is a special method:
If you do not define a constructor method, JavaScript will add an empty constructor method.
Class methods are created with the same syntax as object methods.
Use the keyword class
to create a class.
Always add a constructor()
method.
Then add any number of methods.
Create a Class method named "age", that returns the Car age: