2.13Ts静态属性,setter和getter
class Person5 { constructor(private name: string) {} get getName() { return this.name; } } const person5 = new Person5("dell5"); console.log(person5.getName); // 单例模式 class Demo { private static instance: Demo; private constructor(public name: string) {} static getInstance() { if (!this.instance) { this.instance = new Demo("dell lee"); } return this.instance; } //把这个方法直接挂在类上,而不是类的实例上 } const demo1 = Demo.getInstance(); const demo2 = Demo.getInstance(); // 构建了单例 // demo1 == demo2 // true console.log(demo1.name); console.log(demo2.name);