如果我在webstorm中编写这样的代码
export class someOne {
constructor(param) {
this.test = param;
}
useTest(){
return this.test;
}
}
console.log(new someOne(123).useTest());
并将鼠标移到" this.test"我看到警告:"元素未导出"
这是什么意思? 如果我将.test
更改为.test1
警告消失
答案 0 :(得分:9)
对我来说,它标志着所有"私人"带有前缀下划线的属性。显然,Webstorm / IntelliJ将属性识别为不应导出的东西。
export class someOne {
constructor(param) {
this._test = param;
}
useTest(){
return this._test;
}
}
console.log(new someOne(123).useTest());
答案 1 :(得分:4)
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());