【问题标题】:Equivalent of private variable (leading underscore) in es6?等效于 es6 中的私有变量(前导下划线)?
【发布时间】:2016-01-19 20:06:06
【问题描述】:

我这里有这段代码:

  getData(value, index) {
    const {responseMetadata, responseData} = this.getResponseDatum();
    return responseData.get(index).get('code').toUpperCase();
  }

eslint报错:

  19:12  "responseMetadata" is defined but never used 

在 python 中,我可以通过将变量重命名为_responseMetadata 来消除这种错误。 es6 中有 Equivalent 吗?

【问题讨论】:

  • 如果它不被使用,为什么你需要定义它?在 JS 中拥有未使用的东西并不是错误...
  • 这是getResponseDatum 的函数签名,它返回一个字典。您必须接受有时我们不能随意更改某些 API。
  • 签名不用填写const responseData = this.getResponseDatum().responseData;const {responseData} = this.getResponseDatum();都可以...
  • 请把它变成一个答案,我会接受的。谢谢
  • FWIW,这与 ES6 无关。这完全取决于 eslint 的可配置性。

标签: javascript ecmascript-6 eslint


【解决方案1】:

如果您不需要该变量,请不要创建它:

const {responseData} = this.getResponseDatum();

解构赋值不需要匹配返回对象的所有属性。

在您的情况下,由于您只需要一个属性并且不多次使用它,因此实际上根本没有太多理由使用解构或变量:

getData(value, index) {
    return this.getResponseDatum().responseData.get(index).get('code').toUpperCase();
}

【讨论】:

    【解决方案2】:

    您可以关闭一段代码的规则。见http://eslint.org/docs/user-guide/configuring.html#configuring-rules

    /*eslint-disable */
    
    //suppress all warnings between comments
    alert('foo');
    
    /*eslint-enable */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      相关资源
      最近更新 更多