Is there any kind of hash code function in JavaScript?

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

    Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol

    you can use Es6 symbol to create unique key and access object. Every symbol value returned from Symbol() is unique. A symbol value may be used as an identifier for object properties; this is the data type's only purpose.

    var obj = {};
    
    obj[Symbol('a')] = 'a';
    obj[Symbol.for('b')] = 'b';
    obj['c'] = 'c';
    obj.d = 'd';
    

提交回复
热议问题