Javascript Objects Cannot Have Complex Index Values

Posted November 20, 2008 at 9:08 AM

Tags: Javascript / DHTML

This was just a shot in the dark, but I wanted to see if Javascript Objects could be indexed by other objects rather than just strings (yes I know Strings are technically objects too). I wanted to see if something along these lines would work:

 Launch code in new window » Download code as text file »

  • var objIndex = {
  • DOMElement: { ... },
  • DOMElement: { ... },
  • DOMElement: { ... },
  • ... etc ...
  • };

Basically, I wanted to see if Javascript objects could act like Java HashTables that can take any type of object as an index value.

After some quick testing, I realized that this did not work. At first, though, I thought it might because no errors were thrown. Javascript actually lets you do this; but, not in the I wanted - what it actually does (from what I can gather) is that it creates an index value in the following manner:

 Launch code in new window » Download code as text file »

  • var objIndex = {
  • DOMElement: { ... }
  • };

... becomes:

 Launch code in new window » Download code as text file »

  • var objIndex = {
  • DOMElement.toString(): { ... }
  • };

I believe that Javascript objects requires string-based indexing and therefore takes any object that you pass to it (to be used as an index value) and converts it to a string. Part of the problem is that this string version of the object is rather generic. The P and SPAN tags convert to the following regardless of markup (in FireFox):

[object HTMLParagraphElement]
[object HTMLSpanElement]

Therefore, all P and SPAN tags would be indexed using the same values (respectively) no matter how many there were in the page.

Anyway, it was just a thought.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Nov 20, 2008 at 10:25 AM // reply »
2 Comments

If you override an object's toString() method, then you can control how JavaScript sets the key.

domElement.toString = function() {
return this.id;
};

Sadly, IE doesn't let you override for DOM objects. But this works for custom objects in all browsers.


Nov 20, 2008 at 10:41 AM // reply »
6,516 Comments

@Dave,

That's an interesting approach. To be honest, though, I just discovered a jQuery method that will make this path almost completely unnecessary:

http://www.bennadel.com/index.cfm?dax=blog:1404.view

While not exactly the same thing, the jQuery Data() method allows us to store arbitrary data with any DOM element. Bad ass!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »