2016-11-15 86 views
0

es5中以下代碼的等效代碼是什麼?es5中以下代碼的等效代碼是什麼?

constructor(props) { 
    super(props); 

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

'this.state = Object.assign({},this.props)' – chenkehxx

+0

我覺得Object.assign仍然被認爲ES6。 http://www.ecma-international.org/ecma-262/6.0/#sec-object.assign。隨着ES5你只需要所做的一切複製或下劃線使用助手或lodash –

+0

你總是可以讓巴貝爾編譯到ES5代碼,看看它做什麼。 – jfriend00

回答

1

該代碼看起來像這樣,而不使用任何> = ES6語法。

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

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

巴貝爾的網站has a repl你可以用它來看看編譯後的代碼看起來到底是什麼樣。

在這種情況下,it's quite complex因爲它大部分都包含在Babel用於爲ES5填充ES6類的類實用程序中。


this.state = { editFlag : false, ...this.props }的第二個例子是類似的。

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