在es6

时间:2017-05-30 10:59:09

标签: javascript ecmascript-6 es6-class

我仍然是es6和OOP方法的新手,在玩React时,这个问题在我脑海中浮现。

正如问题标题所说,我想知道将值赋给尚未在构造函数中声明的成员变量是否是一个好习惯。

所以,例如:

class MyClass {
    constructor(props) {
        this._props = props;
    }

    render() {
        // declared in constructor
        console.log(this._props);

        // new member variable
        this._anotherProp = 'New value';
        console.log(this._anotherProp);

    }
}

所以在这种情况下,在渲染方法中声明this._anotherProp是否可以,或者这样做是不好的做法?如果有任何?

,那么在构造函数外部分配成员变量的推荐方法是什么?

我在定义Refs(https://facebook.github.io/react/docs/refs-and-the-dom.html)时在React中看到了类似的方法,尽管我不确定它是否代表与我的示例相同的用例。

0 个答案:

没有答案