Es2015的卷发腕带 [英] Curly Brakets in Es2015
问题描述
我正在阅读一些推文,我发现了这个推文Dan Abromov
I was reading some tweets and I came across this tweet by Dan Abromov
语法令我困惑。
const Font = ({ children }) =>
<Block...
{} / strong>周围的孩子?显然它不是一个对象。我假定它的ES2015功能。
What is the point of { } around children? Clearly its not an object. I presume its ES2015 feature.
非常感谢
推荐答案
破坏绑定模式。它表示参数 children
应绑定到传递给函数的对象的 children
属性的值。
It's a destructuring binding pattern. It indicates that the parameter children
should be bound to the value of the children
property of an object passed to the function.
在ES2015环境中尝试这样做:
Try this in an ES2015 environment:
function x({ foo }) {
console.log(foo);
}
x({ hello: "world", foo: "bar", well: "that's all"});
字符串bar将被记录到控制台,因为这是foo的值传递给函数的对象的属性。
The string "bar" will be logged to the console, because that's the value of the "foo" property of the object passed to the function.
如果传递给函数的值是没有children属性的对象,如果它不是一个对象,那么参数将为 undefined
。
If the value passed to the function is an object with no "children" property, of if it's not an object at all, then the parameter will be undefined
.
这篇关于Es2015的卷发腕带的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!