JavaScript class
Example
Create a Car class, and then an object (myCar) based on the Car class:
// Create a Car class
class Car {
constructor(brand) {
this.carname = brand;
}
}
// Create a Car Object
myCar = new Car("Ford");
Definition and Usage
A class
is a type of object template.
The class
statement initiates a JavaScript class.
Properties and methods are assigned in the constructor()
method.
The constructor()
method is called each time a class object is initialized.
See Also:
Notes
The syntax in a class must be written in "strict mode".
Unlike functions, class declarations are not hoisted (you must declare a class before you can use it).
Syntax
class className {
// class body
}
Related Pages
JavaScript Tutorial: JavaScript Classes
JavaScript Tutorial: JavaScript ES6 (EcmaScript 2015)
JavaScript Tutorial: JavaScript this
JavaScript Tutorial: JavaScript Strict Mode
Browser Support
class
is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
class
is not supported in Internet Explorer 11 (or earlier).