1

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:

  1. I can assign like so instance['key1'] = 'foo'; and read like this instance.key1; I don't want this!!

  2. 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.

Community
  • 1
  • 1
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • Why? Why not just `var instance = {};`? – Jon Hanna Aug 29 '12 at 09:02
  • @JonHanna, because the Dictionary will be part of a 'strongly' (if that can be used with JS) typed library, where Dictionary would represent a similar structure to that in say, Java or C#. Just defining instance as { } would break the library structure since { } could apply to any other object type as well. – Matthew Layton Aug 29 '12 at 09:25
  • So far you've described a normal javascript object though. Since all objects can do this, maybe just create a type of your type-matching (if any), and that's that? – Jon Hanna Aug 29 '12 at 09:30
  • @JonHanna there's a little more to it. The first aspect is to "shim" any "native" js objects which are unedfined. Then the objects will be assigned additional functionality, and formed into a hierarchy using an inheritance model. Finally objects like Dictionary will be added to the hierarchy as well. Whilst I understand that it is a normal object, it must inherit functionality from its ancestors (and override where necessary) and add additional functionality. Hope this makes sense :-S – Matthew Layton Aug 29 '12 at 10:01
  • I think so, but you're still adding functionality it already has here, which is where I'm not seeing the point. All javascript objects inherit the ability to be dictionaries, so what is it you are actually adding? – Jon Hanna Aug 29 '12 at 10:11
  • @JonHanna, take a look at my edit...that might help explain it. – Matthew Layton Aug 29 '12 at 10:17
  • I think so, and I might have an answer now. – Jon Hanna Aug 29 '12 at 10:28

1 Answers1

2
function Dictionary()
{
    this.store = {};    
    this.setValue = function(key, value) {
        store[key] = value;
    }
    this.getValue = function(key)
    {
        return store[key];
    }
    return this;
}
//Testing
var dict = Dictionary();
dict.setValue('key','value');
alert(dict.getValue('key'));//displays "value"
alert(dict.getValue('unassignedKey'));//displays "undefined"
alert(dict['key']);//displays "undefined" (an unfortunate lack of syntactic convenience, we have to go through getValue).
alert(dict.key);//displays "undefined"
var dict2 = Dictionary();
dict2.setValue('key2', 'value2');
alert(dict2.getValue('key'));//displays "undefined"
alert(dict2.getValue('key2'));//displays "value2"
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • alert(dict['key']);//displays "undefined" (an unfortunate lack of syntactic convenience, we have to go through getValue)...it's getting there...but it still needs this really. I may have to play with protecting the functions from being overridden! – Matthew Layton Aug 29 '12 at 11:58
  • That requires you to overload the `[]` operator, which you can't do in javascript. See http://stackoverflow.com/questions/1711357/how-would-you-overload-the-operator-in-javascript – Jon Hanna Aug 29 '12 at 12:08
  • Ah, that answers my question! Thank you! – Matthew Layton Aug 29 '12 at 15:10