immutable vs spread operator ...

115 阅读1分钟

Original unchanged, return a new one with same key merged, like spread operator...

当然上面的做法可以通过spread operator...完全来实现:

const original ={x:123, y:456}
const newx={...original, ...{y:789,z:'abc'}};
console.log(newx)
 {
  x: 123,
  y: 789,
  z: "abc"
}

注意:

  1. spread operator 跟arguments里面的rest不一样,不是一个东西。
  2. ...可以理解成:把元素spread开来,或者expand开来,用来可以被iterate的元素。 关键词:expand+iterable.

理解了上面的关键词于是,下面的好理解了:

重点看这个例子 stackoverflow.com/questions/5… 关于immutable