ya_*_*mon 29 javascript warnings webstorm ecmascript-6
如果我在webstorm中写这样的代码
export class someOne {
constructor(param) {
this.test = param;
}
useTest(){
return this.test;
}
}
console.log(new someOne(123).useTest());
Run Code Online (Sandbox Code Playgroud)
和鼠标悬停在"this.test"上我看到警告:"元素未导出"
这是什么意思?如果我改变.test以.test1警告消失
小智 9
对我而言,它使用带前缀的下划线标记所有"私有"属性.显然,Webstorm/IntelliJ将属性识别为不应导出的东西.
export class someOne {
constructor(param) {
this._test = param;
}
useTest(){
return this._test;
}
}
console.log(new someOne(123).useTest());
Run Code Online (Sandbox Code Playgroud)
Webstorm 只是试图阻止您添加未指定的属性。您需要定义 getter/setter。这可以防止添加和获取属性作为脏黑客。
更新 - 添加了 WeakMap 以使变量真正私有。
let testWeakMap = new WeakMap();
export class someOne {
constructor(param) {
this.test = param;
}
useTest(){
return this.test;
}
get test () {
return testWeakMap.get(this);
}
set test (value) {
testWeakMap.set(this, value);
}
}
console.log(new someOne(123).useTest());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21495 次 |
| 最近记录: |