【发布时间】:2018-04-26 03:49:27
【问题描述】:
name: string;
constructor(private value: string) {
this.name = value;
// or
this.name = this.value;
}
这些选项中哪个更好。为什么我可以选择在value 上使用this 前缀?在构造函数的参数上使用this关键字是否有效?
我在 tsconfig 和 tslint 中使用了noUnusedParameters、noUnusedLocals 来确保我的程序中没有未使用的变量。不幸的是,如果前面没有this,tslint 会报告构造函数的参数(将它们标记为未使用,这很奇怪)。
【问题讨论】:
-
this.value不是undefined? -
@evolutionxbox 您的 jsbin 链接是 javascript,问题是询问打字稿。看到
private关键字了吗?与众不同。 -
@Duncan 输入
private输出首先使用this.value = value的JS。这似乎毫无意义。
标签: javascript typescript constructor tslint tsconfig