我正在阅读Typescript手册,发现了一段(对我来说)毫无意义的代码。有人能解释一下这段代码的目的吗?
取自文档:
取自文档:
class Greeter {
static standardGreeting = "Hello, there";
greeting: string;
greet() {
if (this.greeting) {
return "Hello, " + this.greeting;
}
else {
return Greeter.standardGreeting;
}
}
}
var greeter1: Greeter;
greeter1 = new Greeter();
alert(greeter1.greet());
var greeterMaker: typeof Greeter = Greeter; // This line
greeterMaker.standardGreeting = "Hey there!"; // This line
var greeter2:Greeter = new greeterMaker(); // And this line
alert(greeter2.greet());
所以,文档中说var greeterMaker: typeof Greeter = Greeter
“将保留类本身”。我也不是很确定这是什么意思。
然后它执行以下操作:
greeterMaker.standardGreeting = "Hey there!";
// Which does the exact same thing as this:
Greeter.standardGreeting = "Hey there!";
我在这里漏掉了什么?
Greeter.standardGreeting =“嘿!”;
来避免,但是想象一下将类传递给某个不知道它的模块,你就会明白了。 - Esteban