可以在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
函数在编译时是公开的?