Consider the following article explaining hashtables / dictionaries in JavaScript:
Can anyone recommend a good Hashtable implementation in Javascript?
Given the accepted answer, I want to be able to do this:
var instance = new Dictionary();
instance['key1'] = 'Foo';
instance['key2'] = 'Bar';
instance['key3'] = 123;
instance['key4'] = true;
but I want the key/value pairs to point to an object within the Dictionary internally. consider the following code structure
var Dictionary = function() {
var dictionary; // <-- key value pairs should point to this, not to the Dictionary function;
this.setValue = function(key, value) {
dictionary[key] = value;
}
this.getValue = function() {
return dictionary[key];
}
}
Is this possible?
EDIT:
One way I thought of designing the Dictionary object was like so:
var Dictionary = function() {
this.setValue = function(key, value) {
this[key] = value;
}
this.getValue = function(key) {
return this[key];
}
}
The problems with this are:
I can assign like so instance['key1'] = 'foo'; and read like this instance.key1; I don't want this!!
I can assign this instance['getValue'] = null; and then cannot get a value back because the function is now null!!
Neither of the above should occurr, hence the reason that the set and get functionality should apply to the internal object, not to the dictionary itself.