简体   繁体   English

销毁散布的javascript参数

[英]Destructuring a spreaded javascript parameter

This code 此代码

const foo = {props: {a: 'b'}};

const bar = (props) => {console.log({new: 'object', props})}

bar(foo)

results in the same exact output if i change (props) => to ({...props}) => 如果我将(props) =>更改为({...props}) =>则会得到相同的准确输出

What exactly is happening here? 这里到底发生了什么?

The reason i am asking is because i am doing something similar in React with a pure component. 我问的原因是因为我正在用纯组件在React中做类似的事情。

 export default ({ ...props }) => (
  <BaseLayout {...props} theme={defaultTheme} />
);

If i remove the spread operator in the parameter the component no longer renders. 如果我在参数中删除了散布运算符,则该组件将不再呈现。

I would appreciate if someone could explain exactly what is going on in both examples and if there is any difference between the JSX code and regular javascript, thanks. 如果有人可以准确解释两个示例中发生的事情,以及JSX代码和常规javascript之间有任何区别,我将不胜感激,谢谢。

It creates a shallow clone of an object like Object.assign() did but in a cleaner way. 它像Object.assign()一样创建对象的浅表副本 ,但方式更简洁。

You can look at many use cases here: https://dmitripavlutin.com/object-rest-spread-properties-javascript/ 您可以在此处查看许多用例: https : //dmitripavlutin.com/object-rest-spread-properties-javascript/

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

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