我是JavaScript新手,因爲我來自強類型語言,所以我選擇了TypeScript。Typescript - 靜態屬性創建優化
我想知道在此代碼中變量red
在Red getter
中會發生什麼。是否會在每次通話時重新創建(如Green getter
)或創建一次並每次使用?哪個最好?
class Color {
public R: number;
public G: number;
public B: number;
public A: number;
static get Red(): Color {
var red = new Color(255, 0, 0);
Color.Red = function() { return red; }
return red;
}
static get Green(): Color {
return new Color(0, 255, 0);
}
constructor(red: number, green: number, blue: number, alpha: number = 255) {
this.R = red;
this.G = green;
this.B = blue;
this.A = alpha;
}
}
+1不錯的完整答案。 – Fenton 2013-04-12 09:57:42