简体   繁体   English

有人可以向我解释此es6语法吗?

[英]Can someone explain this es6 syntax to me?

Can someone explain the following statement? 有人可以解释以下陈述吗?

  return state.map(todo =>
    (todo.id === action.id)
      ? {...todo, completed: !todo.completed}
      : todo
  )

More specifically this line 更具体地说,这条线

{...todo, completed: !todo.completed} {...待办事项,已完成:!todo.completed}

Why is there two arguments in the true portion of the ternary operation? 为什么在三元运算的真实部分中有两个参数?

What is ? 什么是 ?

... ...

The ...todo , is the spread syntax, meaning the completed: !todo.completed property will be added to the existing todo object along with the previous properties. ...todo是传播语法,表示completed: !todo.completed属性将与以前的属性一起添加到现有的todo对象中。 Using it you don't have to manually copy over the existing properties. 使用它,您不必手动复制现有属性。

Treat it as expanding the 'todo' object. 将其视为扩展 “待办事项”对象。

Also (todo.id === action.id) is checking whether the id is same in both objects and then adding the completed: !todo.completed property else keeping the old object as it is. 另外(todo.id === action.id)正在检查两个对象中的id是否相同,然后添加completed: !todo.completed属性,否则将旧对象保持原样。

Please check out this reference guide: 请查看此参考指南:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_syntax

{
  ...todo,
  completed: !todo.completed
}

is simply making a copy of todo but with a new completed property. 只是复制todo但具有新的completed属性。 In this case, it's equivalent to the inverse of todo 's completed property. 在这种情况下,它等效于todo的completed属性的倒数。 It is a plain JS Object. 这是一个普通的JS对象。

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

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