//Definition of class
class User {
constructor(typeOfUser){
this._canEditEverything = false;
if (typeOfUser === "administrator") {
this._canEditEverything = true;
}
} get canEditEverything() { return this._canEditEverything; }
}
//Instatiation
let u1 = new User("normalGuy");
let u2 = new User("administrator");
工廠方法
一個父類別,一個子類別
首先建立一個父類別,內要有一個變數,內也要有一個function
經由子類別覆寫的方式,建立出新物件,並且可以使用父類別的方法
//Class
class User {
constructor(){
this._canEditEverything = false;
}
get canEditEverything() { return this._canEditEverything; }
}
//Sub-class
class Administrator extends User {
constructor() {
super();
this._canEditEverything = true;
}
}
//Instatiation
let u2 = new Administrator();
let result = u2.canEditEverything; //true