webstorm:"元素未导出"警告意味着什么?

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"上我看到警告:"元素未导出"

元素未导出wtf

这是什么意思?如果我改变.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)


Nie*_*eek 5

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)

  • 或者,在“编辑器”>“检查”>“Javascript”>“常规”>“未解析的 JavaScript 变量”中禁用“将未声明的属性报告为错误”(PHPStorm 2016.1) (12认同)