最近开始学习Typescript。我对从 Typescript 到 Javascript 的转换有疑问。
为什么这个代码:
class Greeter {
greeting: string;
private hello(){
return this.greeting;
}
public hi(){
alert(this.hello());
}
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
转换成
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.hello = function () {
return this.greeting;
};
Greeter.prototype.hi = function () {
alert(this.hello());
};
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
})();
不是这个?
var Greeter = (function () {
var hello = function(){
return this.greeting;
}
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.hi = function () {
alert(hello.call(this));
};
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
})();
为什么会这样转换?