Last active
August 29, 2015 14:03
-
-
Save m92o/c82a12fe0e789af87c74 to your computer and use it in GitHub Desktop.
Effective JavaScript 項目45
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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