简体   繁体   English

对象解构赋值如何工作?

[英]How object destructuring assignments work?

这个语句怎么赋值?

var { AppRegistry, View, StyleSheet, ListView } = React;

This is destructuring assignment that is coming in ECMAScript 6.这是 ECMAScript 6 中的解构赋值

var s = { a: 1, b: 2 }
var { a, b } = s;

will assign 1 to a and 2 to b .1分配给a并将2分配给b

Presumably, React is an object that has properties AppRegistry , View etc., and their values are being assigned to variables with the same name.据推测, React是一个具有AppRegistryView等属性的对象,它们的值被分配给同名的变量。

This syntax is, at the moment of this answer, not yet widely available.在此答案中,此语法尚未广泛使用。

This is a new feature in ES6 as Amadan said.正如 Amadan 所说,这是 ES6 中的一个新功能。 ES6 will be released soon and all modern browsers will support it eventually. ES6 即将发布,所有现代浏览器最终都会支持它。 A lot of projects use Babel together with webpack to compile code to ES5 synatax so that it can be used on any browsers.很多项目使用Babel和 webpack 将代码编译为 ES5 语法,以便它可以在任何浏览器上使用。

React native have their own transforms which support part of ES6: React Native 有自己的转换,支持 ES6 的一部分:

https://facebook.github.io/react-native/docs/javascript-environment.html#content https://facebook.github.io/react-native/docs/javascript-environment.html#content

So you don't need Babel and webpack if you are using their packager.所以如果你使用他们的打包器,你就不需要 Babel 和 webpack。 In the future version facebook may use Babel as well.在未来的版本中,facebook 也可能会使用 Babel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM