Because if you do, you're creating an entirely new prototype object every time you create a new instance, which completely defeats the point. You only need one prototype object. You're essentially making a hashtable, too, so you might as well call it one and pass an object, not a string, as the initial data:
Code:
function Hash(/* Object */ data) {
if(data)
for(var x in data)
if(data.hasOwnProperty(x))
this[x] = data[x];
}
The rest of your code is superfluous, as the functionality is provided natively. You could, however, add some more handy methods:
Code:
function zip(/* Array arr1, Array arr2, ... */) {
var i, j, r = [], v, iter = 0;
for (i = 0; i < arguments.length; ++i)
iter = Math.max(arguments[i].length, iter);
for (i = 0; i < iter; ++i) {
for (j = 0, v = []; j < arguments.length; ++j)
v.push(arguments[j][i]);
r.push(v);
}
return r;
}
Hash.prototype = {
'keys' : function() {
var r = [];
for(var x in this)
if(this.hasOwnProperty(x))
r.push(x);
return r;
},
'values' : function() {
var r = [];
for(var i = 0, k = this.keys(), n = k.length; i < n; ++i)
r.push(this[k[i]]);
return r;
},
'items' : function() {
return zip(this.keys(), this.values());
},
'size' : function() {
return this.keys().length;
},
'map' : function(/* function(key, value) */ f) {
var r = [];
for(var i = 0, k = this.keys(), n = k.length; i < n; ++i)
r.push(f(k[i], this[k[i]]));
return r;
},
'extend' : function(/* Hash */ newData) {
var r = new Hash(this);
for(var i = 0, k = newData.keys(), n = k.length; i < n; ++i)
r[k[i]] = newData[k[i]];
return r;
}
};
Bookmarks