Curly Brakets in Es2015 [duplicate]

蓝咒 提交于 2019-12-22 08:22:45

问题


I was reading some tweets and I came across this tweet by Dan Abromov

The syntax has me confused.

const Font = ({ children }) => 
 <Block...

What is the point of { } around children? Clearly its not an object. I presume its ES2015 feature.

Many thanks


回答1:


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.

Try this in an ES2015 environment:

function x({ foo }) {
  console.log(foo);
}

x({ hello: "world", foo: "bar", well: "that's all"});

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.

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.



来源:https://stackoverflow.com/questions/35190064/curly-brakets-in-es2015

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!