es5 中以下代码的等效项是什么?

What will be equivalent of following code in es5?

以下代码在 es5 中的等效项是什么?

constructor(props) {
  super(props);

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

如果不使用任何 >= 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 因为它主要包含在 class 实用程序中,Babel 用来为 ES5 填充 ES6 classes。


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

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