Immutable.js 引用相等
Immutable.js reference equality
给定以下简单代码:
const Immutable = require('immutable');
const a = Immutable.fromJS({
a: 1,
b: [2, 3, 4],
c: {
d: 1
}
});
const b = a.setIn(['c', 'd'], "Something else");
const c = b.setIn(['c', 'd'], 1);
console.log(a.equals(b)); // true
console.log(Immutable.is(a, c)); // true
console.log(a === c); // false?
对于最后的比较,我希望它 return 正确,因为我将路径 ['c', 'd']
设置为其他内容,然后返回原始值,并且通过结构共享我会期望它导致 c
持有对原始数据结构的引用吗?
我是否误解了它的工作原理?
第一,这个console.log(a.equals(b));
returns其实是假的:
现在回答您的问题,如 Immutable.js here 的 "Return self on no-op optimization" 子章所述:
When possible, Immutable.js avoids creating new objects for updates
where no change in value occurred, to allow for efficient reference
equality checking to quickly determine if no change occurred.
有那个例子:
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.
However updates which do result in a change will return a new
reference. Each of these operations occur independently, so two
similar updates will not return the same 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
returns 一个新的参考。因此它们在引用上不相等。
希望我有所帮助:)
给定以下简单代码:
const Immutable = require('immutable');
const a = Immutable.fromJS({
a: 1,
b: [2, 3, 4],
c: {
d: 1
}
});
const b = a.setIn(['c', 'd'], "Something else");
const c = b.setIn(['c', 'd'], 1);
console.log(a.equals(b)); // true
console.log(Immutable.is(a, c)); // true
console.log(a === c); // false?
对于最后的比较,我希望它 return 正确,因为我将路径 ['c', 'd']
设置为其他内容,然后返回原始值,并且通过结构共享我会期望它导致 c
持有对原始数据结构的引用吗?
我是否误解了它的工作原理?
第一,这个console.log(a.equals(b));
returns其实是假的:
现在回答您的问题,如 Immutable.js here 的 "Return self on no-op optimization" 子章所述:
When possible, Immutable.js avoids creating new objects for updates where no change in value occurred, to allow for efficient reference equality checking to quickly determine if no change occurred.
有那个例子:
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.
However updates which do result in a change will return a new reference. Each of these operations occur independently, so two similar updates will not return the same 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
returns 一个新的参考。因此它们在引用上不相等。
希望我有所帮助:)