JavaScript class declaration
- Previous Page break
- Next Page const
- Go to the Previous Level JavaScript Statement Reference Manual
Definition and usage
A class is a function, but it is initialized not with the keyword function, but with the keyword class, and attributes are assigned in the constructor() method.
The constructor() method is called each time a class object is initialized.
Note:Unlike functions and other JavaScript declarations, class declarations are not hoisted (you must declare a class first before you can use it).
Note:The syntax in the class must be written in "strict mode".
For more information about classes, please read our JavaScript classes tutorial.
Instance
Create a Car class and then create an object named "mycar" based on the Car class:
class Car { // Create class constructor(brand) { // Constructor this.carname = brand; // Class body } } mycar = new Car("Ford"); // Create an object of Car class
Syntax
class className { // Class body }
Technical details
JavaScript version: | ECMAScript 2015 (ES6) |
---|
Browser support
The following table defines the first browser versions that fully support JavaScript classes:
Keywords | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
class | 49.0 | 12.0 | 45.0 | 9.0 | 36.0 |
Related pages
JavaScript Tutorial:JavaScript Class
JavaScript Tutorial:JavaScript ES6 (EcmaScript 2015)
JavaScript Tutorial:JavaScript this
JavaScript Tutorial:JavaScript Strict Mode
- Previous Page break
- Next Page const
- Go to the Previous Level JavaScript Statement Reference Manual