我正在查看Immutable.js的源代码,其中有一个我不理解的ownerID
字段。
这是Map.asMutable()
和Map.asImmutable()
的源代码:https://github.com/facebook/immutable-js/blob/master/src/Map.js#L171
似乎可变对象和不可变对象之间唯一的区别就是它们的ownerID
。什么是ownerID
,它是用来做什么的?
发布于 2016-03-24 11:54:44
// A function which returns a value representing an "owner" for transient writes
// to tries. The return value will only ever equal itself, and will not equal
// the return of any subsequent call of this function.
function OwnerID() {}
我对上面的理解是this.__ownerID
字段是用来比较对象的。与自身进行比较的Map
将具有相同的ownerID
,而与另一个Map
进行比较的Map
将看到两个不同的ownerID
。
您可以在little farther down in the file in question中查看此用法的示例
__ensureOwner(ownerID) {
if (ownerID === this.__ownerID) {
return this;
}
if (!ownerID) {
this.__ownerID = ownerID;
this.__altered = false;
return this;
}
return makeMap(this.size, this._root, ownerID, this.__hash);
}
实际上,在searching the entire repo中,您将看到该函数在数据类型中是通用的,每种类型都有一个略微修改的版本,以返回该类型的正确新版本。
https://stackoverflow.com/questions/35998781
复制相似问题