immutablejs源码中这个计算json对象哈希的的算法的怎么理解?
immutablejs源码中这个计算json对象的的算法的怎么理解?为什么hashJSObj({a: 2})
为1?它究竟有什么用,为了实现什么?,为什么实例里hash出来的是一个数字?
function hashJSObj(obj) {
var hash;
if (usingWeakMap) {
hash = weakMap.get(obj);
if (hash !== undefined) {
return hash;
}
}
hash = obj[UID_HASH_KEY];
if (hash !== undefined) {
return hash;
}
if (!canDefineProperty) {
hash = obj.propertyIsEnumerable && obj.propertyIsEnumerable[UID_HASH_KEY];
if (hash !== undefined) {
return hash;
}
hash = getIENodeHash(obj);
if (hash !== undefined) {
return hash;
}
}
hash = ++objHashUID;
if (objHashUID & 0x40000000) {
objHashUID = 0;
}
if (usingWeakMap) {
weakMap.set(obj, hash);
} else if (isExtensible !== undefined && isExtensible(obj) === false) {
throw new Error('Non-extensible objects are not allowed as keys.');
} else if (canDefineProperty) {
Object.defineProperty(obj, UID_HASH_KEY, {
'enumerable': false,
'configurable': false,
'writable': false,
'value': hash
});
} else if (obj.propertyIsEnumerable !== undefined &&
obj.propertyIsEnumerable === obj.constructor.prototype.propertyIsEnumerable) {
// Since we can't define a non-enumerable property on the object
// we'll hijack one of the less-used non-enumerable properties to
// save our hash on it. Since this is a function it will not show up in
// `JSON.stringify` which is what we want.
obj.propertyIsEnumerable = function() {
return this.constructor.prototype.propertyIsEnumerable.apply(this, arguments);
};
obj.propertyIsEnumerable[UID_HASH_KEY] = hash;
} else if (obj.nodeType !== undefined) {
// At this point we couldn't get the IE `uniqueID` to use as a hash
// and we couldn't use a non-enumerable property to exploit the
// dontEnum bug so we simply add the `UID_HASH_KEY` on the node
// itself.
obj[UID_HASH_KEY] = hash;
} else {
throw new Error('Unable to set a non-enumerable property on object.');
}
return hash;
}
// v8 has an optimization(优化组合) for storing 31-bit signed numbers(有正负符号数).
// Values which have either 00 or 11 as the high order bits(高字节位) qualify(限定).
// This function drops the highest order bit in a signed number(有正负符号数), maintaining(坚持,保卫)
// the sign bit(符号位).
function smi(i32) {
return ((i32 >>> 1) & 0x40000000) | (i32 & 0xBFFFFFFF);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论