JavaScript 类参考
JavaScript 类
A class
is a type of function, but instead of using the keyword function
to initiate it, we use the keyword class
, and the properties are assigned inside a constructor()
method
示例
创建一个 Car 类,然后基于 Car 类创建一个名为 "mycar" 的对象
class Car { // 创建一个类
constructor(brand) { // 类的构造函数
this.carname = brand; // 类主体/属性
}
}
mycar = new Car("Ford"); // 创建 Car 类的对象
另请参阅
类方法
方法 | 描述 |
---|---|
constructor() | 一个用于在类中创建和初始化对象的特殊方法 |
类关键字
关键字 | 描述 |
---|---|
extends | 扩展一个类(继承) |
static | 为类定义一个静态方法 |
super | 指向父类 |