首先,这一个
console.log(a.equals(b));
实际返回假:
现在回答你的问题,如immutable.js中所述。
here
在“返回自我无操作优化”子章:
如果可能的话,immutable.js避免为更新创建新对象
如果值没有发生变化,则允许有效引用
相等性检查以快速确定是否没有发生更改。
举个例子:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 2);
updatedMap === originalMap; // No-op .set() returned the original reference.
但是,会导致更改的更新将返回新的
参考文献。每个操作都是独立进行的,因此有两个
类似的更新不会返回相同的引用:
这个例子:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 1000);
// New instance, leaving the original immutable.
updatedMap !== originalMap;
const anotherUpdatedMap = originalMap.set('b', 1000);
// Despite both the results of the same operation, each created a new reference.
anotherUpdatedMap !== updatedMap;
// However the two are value equal.
anotherUpdatedMap.equals(updatedMap);
因为你在改变价值,
setIn
返回新引用。因此,它们在参考中并不相等。
希望我能帮忙:)