【发布时间】:2016-06-19 14:19:14
【问题描述】:
在 React/React-Native 中使用 ES6+ 语法,变量foo,当在构造函数之外定义时,在使用this. 调用时会以某种方式转换为实例变量。我的断言是真的吗?为什么它甚至可以工作,而不是在构造函数中实例化?这里有一个对应的React Native代码sn-p:
class myComponent extends Component {
constructor() {
super();
}
foo = "bar";
render() {
return ( <View>{ this.foo }</View> );
}
}
This discussion about ES7 property initializers 展示了state 变量是如何在 React/React Native 中以这种方式突出使用的。
【问题讨论】:
-
“为什么它在构造函数中没有被实例化时还能工作?”——因为类字段是一个可怕的想法
-
相关帖子here.
标签: javascript reactjs react-native ecmascript-next