简体   繁体   English

在没有Object.assign的情况下使用ES6 Spread

[英]Using es6 spread without Object.assign

I'm not a big fan of Object.assign, in my opinion, it is hard to read and not elegant, I try to avoid it. 我不是Object.assign的忠实拥护者,我认为它很难阅读且不美观,我尝试避免使用它。 Somewhere I saw this in redux reducer 我在redux reducer中看到的这个地方

case ADD_TODO:
  return Object.assign({}, state, {
    todos: [
      ...state.todos,
      {
        text: action.text
      }
    ]
  })

Just curious is Object assign looks good above? 只是好奇对象分配在上面看起来不错吗? Why not just do this instead? 为什么不这样做呢?

case ADD_TODO:
  return {
    ...state,
    todos: [
      ...state.todos,
      {
        text: action.text
      }
    ]
  }

Why not just do this instead? 为什么不这样做呢?

Because it's not valid ES6 anymore. 因为它不再是有效的ES6。

The object rest/spread syntax proposal had been around for some time but only was accepted for ES2018. 对象剩余/扩展语法建议已经存在了一段时间,但仅在ES2018中被接受。 See here for details. 有关详细信息,请参见此处

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

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