class Handler {
static greet: string = 'hello';
constructor(public info: string) {}
setInfo = (inf: string) => { // 箭头表达式
this.info = inf
}
normalFun = function () {
console.log('我是一个普通函数')
}
myMethod() {
console.log('只有fun()这种形式的方法才会被生成到 prototype上')
}
}
转换后的代码:
"use strict";
var Handler = /** @class */ (function () {
function Handler(info) {
var _this = this;
this.info = info;
this.setInfo = function (inf) {
_this.info = inf;
};
this.normalFun = function () {
console.log('我是一个普通函数');
};
}
Handler.prototype.myMethod = function () {
console.log('只有fun()这种形式的方法才会被生成到 prototype上');
};
Handler.greet = 'hello';
return Handler;
}());
static成员被挂在了构造函数上, 用户箭头函数或者 = function赋值的形式,会变成每个实例都有的属性(这会增加额外的开销), methodName()的形式才会被挂在prototype上
网友评论