JavaScript hashmap equivalent

前端 未结 17 1430
执笔经年
执笔经年 2020-11-22 13:23

As made clear in update 3 on this answer, this notation:

var hash = {};
hash[X]

does not actually hash the object X; it actually

17条回答
  •  长情又很酷
    2020-11-22 14:04

    You can use ECMAScript 6 WeakMap or Map:

    • WeakMaps are key/value maps in which keys are objects.

    Map objects are simple key/value maps. Any value (both objects and primitive values) may be used as either a key or a value.

    Be aware that neither is widely supported, but you can use ECMAScript 6 Shim (requires native ECMAScript 5 or ECMAScript 5 Shim) to support Map, but not WeakMap (see why).

提交回复
热议问题