Use the keyword class
to create a class.
Always add a method named constructor()
:
Syntax
class ClassName {
constructor() { ... }
}
constructor() { ... }
}
Example
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
}
constructor(name, year) {
this.name = name;
this.year = year;
}
}
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.
Practice Excercise Practice now