Javascript webstorm:“元素未导出”警告是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36670910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:23:24  来源:igfitidea点击:

webstorm: what does "Element is not exported" warning mean?

javascriptecmascript-6warningswebstorm

提问by ya_dimon

if i write such code in webstorm

如果我在 webstorm 中编写这样的代码

export class someOne {
  constructor(param) {
    this.test = param;
  }

  useTest(){
    return this.test;
  }
}


console.log(new someOne(123).useTest());

and mouseover on "this.test" i see the warning: "Element is not exported"

鼠标悬停在“this.test”上,我看到警告:“元素未导出”

element is not exported wtf

元素未导出 wtf

what does it mean? if i change the .testto .test1the warning disappears

这是什么意思?如果我更改.test.test1警告消失

回答by Fabian

For me it worked to mark all "private" properties with a prefixed underscore. Obviously Webstorm/IntelliJ recognized the properties as something that should not be exported.

对我来说,它可以用前缀下划线标记所有“私有”属性。显然,Webstorm/IntelliJ 将这些属性识别为不应导出的内容。

export class someOne {
  constructor(param) {
    this._test = param;
  }

  useTest(){
    return this._test;
  }
}


console.log(new someOne(123).useTest());

回答by Niels Steenbeek

Webstorm just tries to prevent you adding unspecified attributes. You need to define getters/setters. This prevents adding and grabbing attributes as dirty hacks.

Webstorm 只是试图阻止您添加未指定的属性。您需要定义 getter/setter。这可以防止添加和获取属性作为脏黑客。

Update - added WeakMap to have variables really private.

更新 - 添加了 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());