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");
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.
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).
class
className {
// class body
}
JavaScript Tutorial: JavaScript Classes
JavaScript Tutorial: JavaScript ES6 (EcmaScript 2015)
JavaScript Tutorial: JavaScript this
JavaScript Tutorial: JavaScript Strict Mode
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).
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!