Is there any kind of hash code function in JavaScript?

后端 未结 20 1178
慢半拍i
慢半拍i 2020-12-04 09:59

Basically, I\'m trying to create an object of unique objects, a set. I had the brilliant idea of just using a JavaScript object with objects for the property names. Such as,

20条回答
  •  遥遥无期
    2020-12-04 10:33

    My solution introduces a static function for the global Object object.

    (function() {
        var lastStorageId = 0;
    
        this.Object.hash = function(object) {
            var hash = object.__id;
    
            if (!hash)
                 hash = object.__id = lastStorageId++;
    
            return '#' + hash;
        };
    }());
    

    I think this is more convenient with other object manipulating functions in JavaScript.

提交回复
热议问题