English 中文(简体)
理解 j印如何工作
原标题:Understanding how javascript hashtables work
  • 时间:2011-08-19 19:31:53
  •  标签:
  • javascript

谁能向我解释为什么以下的守则样本是真实的? 我本会认为,如同在C#中那样,测试1! 测试2。

<>Update: 因此,我认为,我将得到一些独特的识别资料,储存在测试1和测试2的基础。

function Test1() { };

function Test2() { };

var test1 = new Test1();

var test2 = new Test2();

var dict = new Array();

dict[test1] = true;

alert(dict[test2]);
最佳回答

摇篮中的钥匙(基本目标)总是被扼杀。 因此,你所加的任何东西都将转化为扼杀。

new Test1();

查阅<代码> 试验1。 引证:

"[object Object]"

。 事实上,在存储<代码>true时,在<编码>新试验1(的关键部分下,你正在用钥匙new Test2(>作为示意图,与钥匙记录相同。 换言之,

(new Test1()).toString() == (new Test2()).toString();

因此,实际目标是:

{
 "[object Object]": true
}

解决办法如下:

Test1.prototype.toString = function() { return "Test1" };
Test2.prototype.toString = function() { return "Test2" };

然后,dict[test1]> will be Deposit as dict[ Test1] and dict[test2] as dict[ Test2 ], 从而使您得以在它们之间有所不同。 不过,人工设定<代码>[测试1]将取代标准。 我知道,没有任何办法把物体作为钥匙。

问题回答

您的标语(Java 字典)是not,使用test1test2,但代表人数是关键。 自test1test2 代表级别相同:[object Object]>,true 价值与这一关键因素相关。

做如下事情:

function Test1(id) { this.id=id };

function Test2(id) { this.id=id };

var test1 = new Test1( 1 ); 
var test2 = new Test2( 2 ); 
var dict = {};

dict[test1.id] = true;

console.log(dict[test1.id]);

Java印物体是确切的薄膜;它们实际上用string钥匙表示物体。

When you use an object as a key, the object is converted to a string by calling toString().
toString() will return the same string for all custom classes (unless you create your own toString), so they end up using the same key.

<>1> 仅用于数字钥匙。 任何其他东西都使用物体。

财产名称只能加以扼制。 任何其他东西都转化为其代表。 在物体的情况下,见<代码>[目标],或<代码>至编码>。

Which means, that if you want to make both objects distinguishable, you have to override this method and let it return something which is unique to each instance.

This question might help you: Hash/associative array using several objects as key





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签