Skip to content

Instantly share code, notes, and snippets.

@m92o
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m92o/c82a12fe0e789af87c74 to your computer and use it in GitHub Desktop.
Save m92o/c82a12fe0e789af87c74 to your computer and use it in GitHub Desktop.
Effective JavaScript 項目45
function Dict(elements) {
this.elements = elements || {};
this.hasSpecialProto = false;
this.specialProto = undefined;
}
Dict.prototype.has = function (key) {
if (key === "__proto__") {
return this.hasSpecialProto;
}
return {}.hasOwnProperty.call(this.elements, key);
};
Dict.prototype.get = function (key) {
if (key === "__proto__") {
return this.specialProto;
}
return this.has(key) ? this.elements[key] : undefined;
};
Dict.prototype.set = function (key) {
if (key === "__proto__") {
this.hasSpecialProto = true;
this.specialProto = val;
} else {
this.elements[key] = val;
}
};
Dict.prototype.remove = function (key) {
if (key === "__proto__") {
this.hasSpecialProto = false;
this.specialProto = undefined;
} else {
delete this.elements[key];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment