简体   繁体   English

WebStorm / PhpStorm和JavaScript ES6:无法识别解构分配

[英]WebStorm/PhpStorm and JavaScript ES6: destructuring assignment not recognized

Consider this ES6 code: 考虑以下ES6代码:

class Person {
    constructor(first, last) {
        let fixed = this.normalize(first, last);
        this.first = fixed[0];
        this.last = fixed[1];
    }

    normalize(first, last) {
        return [first.toUpperCase(), last.toUpperCase()];
    }
}

PhpStorm/WebStorm has no problem recognizing that first and last are properties of the class when I later use them: 当我以后使用它们时,PhpStorm / WebStorm毫无疑问地认识到firstlast是该类的属性:

在此处输入图片说明

Now if I modify the constructor to use the destructuring assignment syntax : 现在,如果我修改构造函数以使用解构赋值语法

constructor(first, last) {
    [this.first, this.last] = this.normalize(first, last);
}

The code executes without error, but the IDE can no longer see the properties: 该代码将正确执行,但IDE不再能看到属性:

在此处输入图片说明

在此处输入图片说明

Is this a problem with my code? 我的代码有问题吗? My IDE settings? 我的IDE设置? I use PhpStorm 2017.2.4 我使用PhpStorm 2017.2.4

You can manually tell PHPStorm that these properties should be assumed to exist on Person -linked objects by adding a JSDoc to the class: 您可以通过向类添加JSDoc来手动告诉PHPStorm这些属性应假定存在于与Person对象上:

/**
 * @property {String} first
 * @property {String} last
 */
class Person {
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM