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 | 指向父類 |