对象传播与Object.assign

本文翻译自:Object spread vs. Object.assign

Let's say I have an options variable and I want to set some default value. 假设我有一个options变量,并且想要设置一些默认值。

What's is the benefit / drawback of these two alternatives? 这两种选择的优点/缺点是什么?

Using object spread 使用对象传播

options = {...optionsDefault, ...options};

Or using Object.assign 或使用Object.assign

options = Object.assign({}, optionsDefault, options);

This is the commit that made me wonder. 这就是让我感到奇怪的承诺


#1楼

参考:https://stackoom.com/question/2E9Po/对象传播与Object-assign


#2楼

This isn't necessarily exhaustive. 这并不一定是详尽无遗的。

Spread syntax 传播语法

options = {...optionsDefault, ...options};

Advantages: 好处:

  • If authoring code for execution in environments without native support, you may be able to just compile this syntax (as opposed to using a polyfill). 如果编写代码以在没有本机支持的环境中执行,则可以仅编译此语法(与使用polyfill相对)。 (With Babel, for example.) (例如,使用Babel。)

  • Less verbose. 不太冗长。

Disadvantages: 缺点:

  • When this answer was originally written, this was a proposal , not standardized. 最初编写此答案时,这只是一个建议 ,而不是标准化的。 When using proposals consider what you'd do if you write code with it now and it doesn't get standardized or changes as it moves toward standardization. 使用投标时,请考虑一下如果现在用它编写代码会做什么,并且在朝着标准化的方向发展时,它不会变得标准化或更改。 This has since been standardized in ES2018. 此后已在ES2018中标准化。

  • Literal, not dynamic. 从字面上看,不是动态

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值