JavaScript Class Referentiemanual

JavaScript class

类是函数的一种,但我们不使用关键字 function 来对其初始化,而是使用关键字 class,并在 constructor() 方法中分配属性:

实例

创建一个 Car 类,然后基于这个 Car 类创建名为 "mycar" 的对象:

class Car {  // 创建类
  constructor(brand) {  // 类构造方法
    this.carname = brand;  // 类主体、属性
  }
}
mycar = new Car("Ford");  // 创建 Car 类的对象

Try it yourself

For knowledge about classes, please read our JavaScript class tutorial.

Class method

Method Description
constructor() A special method used to create and initialize objects created in the class.

Class keyword

Keyword Description
extends Extend class (inheritance).
static Define static methods for class.
super Reference superclass.