Array as hash tables

Question

Hello,

It is common knowledge that arrays can be used as hashtables:

var color = [];

color["red"] = 0xFF0000;
color["blue"] = 0x0000FF;
With this, I can check whether a string identifies a color, or not:

function isColor(string)
{
return color[string] == undefined;
}

My only concern is that

isColor("pop") == true

and that will hold true for quite a few annoying, parasitic,
"keywords" ("constructor", "length", etc...)

I finally came to use the following as a naked object, to use as a
hashtable:

function HashTable() { this.constructor = undefined; }

// example of use:
var color = new HashTable;
color["red"] = 0xFF0000
Question for experts: won''t then there be other unexpected, hardcoded,
keywords stalking my strings in the dark, and returning obscure,
native, js objects, where I expected plain numbers ?

Any help appreciated,
Alexis

Solution
Alexis Nikichine wrote:
It is common knowledge that arrays can be used as hashtables:

var color = [];

color["red"] = 0xFF0000;
color["blue"] = 0x0000FF;
With this, I can check whether a string identifies a color, or not:

function isColor(string)
{
return color[string] == undefined;
}

My only concern is that

isColor("pop") == true

and that will hold true for quite a few annoying, parasitic,
"keywords" ("constructor", "length", etc...)

I finally came to use the following as a naked object, to use as a
hashtable:

function HashTable() { this.constructor = undefined; }

// example of use:
var color = new HashTable;
color["red"] = 0xFF0000
Question for experts: won''t then there be other unexpected, hardcoded,
keywords stalking my strings in the dark, and returning obscure,
native, js objects, where I expected plain numbers ?

Any help appreciated,


If the indexes are not integers, then use an Object, not an Array.

var color = {};

If the subscript is an identifier (and not a reserved word), you can use
the stylish dot notation.

color.red = 0xFF0000;
color.blue = 0x0000FF;

In HTML applications, it may be better to keep color values as strings.
That way you won''t get a false negative on black.

color.red = "#FF0000";
color.blue = "#0000FF";
color.black = "#000000";

If you use a little type discipline, then it is eay to distinguish your
values from Object methods.

function isColor(string) {
return typeof color[string] == "string";
}

The hashtable constructor above is silly. If it doesn''t have methods,
then it is better to use a plain Object. Even better, you can use the
object literal notation.

color = {red: "#FF0000", blue: "#0000FF", black: "#000000"};

http://www.JSON.org

Douglas Crockford wrote:
If the indexes are not integers, then use an Object, not an Array.

var color = {};

If the subscript is an identifier (and not a reserved word), you can use the stylish dot notation.

color.red = 0xFF0000;
color.blue = 0x0000FF;

In HTML applications, it may be better to keep color [snip>

The color example was just a gratuitous one. I was worrying about
arbitrary strings typed in by users. One day, I had reports of "pop art"
request failing. "pop" yielded a function (instead of an expected
integer).
If you use a little type discipline, then it is eay to distinguish your values from Object methods.

function isColor(string) {
return typeof color[string] == "string";
}
When I first met the, I used type discipline, and made sure that the
returned value was an integer.
The hashtable constructor above is silly. If it doesn''t have methods,
then it is better to use a plain Object. Even better, you can use the
object literal notation.


Unfortunately, I can''t rely on type discipline, now, since (in another
urelated case) I need an hash (and don''t call me a perl boob :-)
returning hash, integers.

With a plain Object, I will get an unexpected result on "constructor".

Which is why I am asking: is my HashTable a really really empty
Object/Function, once the "constructor" member has been blanked ?

Alexis

--
some domain is free (and btw, everything I know, I learned from your
site; thank you :-)

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

>>If the indexes are not integers, then use an Object, not an Array.

var color = {};

If the subscript is an identifier (and not a reserved word), you can


use
the stylish dot notation.

color.red = 0xFF0000;
color.blue = 0x0000FF;

In HTML applications, it may be better to keep color


[snip>

The color example was just a gratuitous one. I was worrying about
arbitrary strings typed in by users. One day, I had reports of "pop art"
request failing. "pop" yielded a function (instead of an expected
integer).

If you use a little type discipline, then it is eay to distinguish


your
values from Object methods.

function isColor(string) {
return typeof color[string] == "string";
}

When I first met the, I used type discipline, and made sure that the
returned value was an integer.

The hashtable constructor above is silly. If it doesn''t have methods,
then it is better to use a plain Object. Even better, you can use the
object literal notation.

Unfortunately, I can''t rely on type discipline, now, since (in another
urelated case) I need an hash (and don''t call me a perl boob :-)
returning hash, integers.

With a plain Object, I will get an unexpected result on "constructor".

Which is why I am asking: is my HashTable a really really empty
Object/Function, once the "constructor" member has been blanked ?


Yes and no. A new object is empty, but it inherits values from its
prototype chain. Clobbering the constructor does not change that.

If the values in your object (or for the boobs, hash) are anything but
functions, you can use

(typeof color[string] != ''function'')

to discriminate your values from the Object methods.

By the way, I think this was a design error in JavaScript. The
appearance of those methods complicates the use of objects as collections.

http://www.crockford.com/javascript/survey.html