使用多个对象作为关键字的哈希/关联数组 [英] Hash/associative array using several objects as key

查看:98
本文介绍了使用多个对象作为关键字的哈希/关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以创建一个关联数组,其中每个键都是几个对象的散列?我不想检查每个对象的状态,而是检查对象的身份。

Is there a way of making an associative array where each key is a hash of several objects? I'm not interested in inspecting each object's state, but rather the object's identity.

 var myarray = {};

 var a = new A();
 var b = new B();
 var c = new C();

 // + is not right, but illustrates the hashing I'm after.
 myarray[a + b + c] = 42;

+ 操作符不正确。在Java中,我会算术结合这三个实例中的每一个的 System.identityHashCode(),并使用结果创建新的散列键。在JavaScript中是否有类似的机制?

The + operator is not right. In java I would arithmetically combine the System.identityHashCode() for each of these three instances and use the result to make my new hash key. Is there some similar mechanic in javascript?

覆盖A,B和C中的 .toString()方法是不是一种选择,因为我对对象标识感兴趣,而不是状态。

Overriding the .toString() method in A, B and C is not an option since I'm interested in object identity, not state.

推荐答案

是字符串,并没有相当于java的对象标识。

Actually impossible since Object keys in this language only can be strings and there's no equivalent of java's object identity.

:o)

这篇关于使用多个对象作为关键字的哈希/关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆