我是 JavaScript 新手,因为我来自强类型语言,所以我选择了 TypeScript。
我想知道这段代码red
中的变量会发生什么。Red getter
它会在每次调用时重新创建(如 in 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;
}
}