什么将等同于es5中的以下代码?

时间:2016-11-15 01:53:36

标签: reactjs ecmascript-6 ecmascript-5

什么等同于es5中的以下代码?

constructor(props) {
  super(props);

  this.state = { ...this.props };
}

1 个答案:

答案 0 :(得分:1)

如果不使用任何> = ES6语法,该代码看起来就像这样。

function MyComponent(props) {
  // super(props)
  React.Component.call(this, props);

  // this.state = { ...this.props };
  this.state = Object.assign({}, props);
}

Babel的网站has a repl,您可以使用它来查看已编译代码的确切内容。

在这种情况下it's quite complex,因为它主要包含在Babel用于为ES5填充ES6类的类实用程序中。

this.state = { editFlag : false, ...this.props }的第二个例子类似。

this.state = Object.assign({}, editFlag: false, this.props);
相关问题