Various Dictionary bugs
cspotcode opened this issue · 1 comments
cspotcode commented
var dict = new buckets.Dictionary();
dict.set("toString", 42);
dict.size() === 0; // wrong, should be 1
dict.remove("toString");
dict.size() === -1; // wrong, should be 0
dict.set("hasOwnProperty", "foo");
try {
dict.keys(); // throws an error trying to invoke "foo" as a function
} catch(e) {
dict.remove("hasOwnProperty");
}
dict.set("__proto__", {value: 123});
dict.get("value") === 123; // wrong, should be undefined
The __proto__
issue can be prevented by prefixing all keys with a weird character like "~" before storing them into table
.
this.table["~" + key] = {.....}
Instead of this.table.hasOwnProperty
, do:
var has = function(obj, key) {
Object.prototype.hasOwnProperty.call(obj, key);
}
has(table, key); // instead of table.hasOwnProperty(key)
When checking whether or not the dictionary has a certain key, always use has()
.
mauriciosantos commented
Fixed. Thank you.