Is there any kind of hash code function in JavaScript?

后端 未结 20 1201
慢半拍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:29

    The solution I chose is similar to Daniel's, but rather than use an object factory and override the toString, I explicitly add the hash to the object when it is first requested through a getHashCode function. A little messy, but better for my needs :)

    Function.prototype.getHashCode = (function(id) {
        return function() {
            if (!this.hashCode) {
                this.hashCode = '';
            }
            return this.hashCode;
        }
    }(0));
    

提交回复
热议问题