Created
August 22, 2013 15:07
-
-
Save tregusti/6308425 to your computer and use it in GitHub Desktop.
Allow easy modification to key/value objects.
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 With( key, value ) { | |
this[key] = value; | |
return this; | |
}; | |
function Except( key ) { | |
delete this[key]; | |
return this; | |
}; | |
exports.augment = function() { | |
if (Object.prototype.with && Object.prototype.with !== With) { | |
throw new Error("Object.prototype already contains a member 'with'."); | |
} | |
Object.prototype.with = With; | |
if (Object.prototype.except && Object.prototype.except !== Except) { | |
throw new Error("Object.prototype already contains a member 'except'."); | |
} | |
Object.prototype.except = Except; | |
} | |
exports.reset = function() { | |
if (Object.prototype.with === With) | |
delete Object.prototype.with; | |
if (Object.prototype.except === Except) | |
delete Object.prototype.except; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment