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" ("constructo r", "length", etc...)
I finally came to use the following as a naked object, to use as a
hashtable:
function HashTable() { this.constructo r = 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
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" ("constructo r", "length", etc...)
I finally came to use the following as a naked object, to use as a
hashtable:
function HashTable() { this.constructo r = 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
Comment