ES6 +实例属性在构造函数之外实例化 [英] ES6+ instance property instantiated outside of constructor

查看:251
本文介绍了ES6 +实例属性在构造函数之外实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在React / React-Native中使用ES6 +语法,变量 foo ,当在构造函数之外定义 时,会以某种方式转换为实例变量用调用时。。我的断言是真的吗?为什么它在构造函数中没有实例化时甚至可以工作?这里有一个相应的React Native代码片段:

Using ES6+ syntax in React/React-Native, the variable foo, when defined outside of the constructor is somehow transformed into an instance variable when called with this.. Is my assertion true? Why does it even work, when not instantiated in the constructor? Here a corresponding React Native code snippet:

class myComponent extends Component {
  constructor() {
    super();
  }

  foo = "bar";

  render() {
    return ( <View>{ this.foo }</View> );
  }
}






关于ES7属性初始化程序的讨论显示了状态变量在React / React Native中以这种方式显着使用。


This discussion about ES7 property initializers shows how the state variable is prominently used in this way in React/React Native.

到目前为止,我通过在哪里这里无法为我回答这个问题..

So far related Stack Overflow discussions I read through here and here could not answer this question for me..

推荐答案

你的断言是真的。

问题是它目前只在阶段-1 中,因此不清楚它何时成为标准。

The problem is that it's currently just in the stage-1, so it's not clear when and if ever it becomes the standard.

参考文献:

  • https://github.com/tc39/ecma262
  • https://github.com/jeffmo/es-class-fields-and-static-properties
  • https://github.com/jeffmo/es-class-fields-and-static-properties#part-1-class-instance-fields

这篇关于ES6 +实例属性在构造函数之外实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆