是否可以在 TypeScript 类中创建私有“函数”(方法)?假设我们有以下Person.ts
TypeScript 文件:
class Person {
constructor(public firstName: string, public lastName: string) {
}
public shout(phrase: string) {
alert(phrase);
}
private whisper(phrase: string) {
console.log(phrase);
}
}
编译时将转换为以下内容:
var Person = (function () {
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.shout = function (phrase) {
alert(phrase);
};
Person.prototype.whisper = function (phrase) {
console.log(phrase);
};
return Person;
})();
观察
我期待whisper
函数在闭包中声明,而不是在原型上?本质上,这会使whisper
函数在编译时公开?