Make writability helper function non-destructive

After checking the writability of a given property, restore the property
value to its original state.
This commit is contained in:
Mike Pennisi 2015-03-04 18:38:37 -05:00
parent 2eca2c71e8
commit 9f8f0284c3
1 changed files with 11 additions and 4 deletions

View File

@ -29,6 +29,9 @@ function isEqualTo(obj, name, expectedValue) {
function isWritable(obj, name, verifyProp, value) {
var newValue = value || "unlikelyValue";
var hadValue = Object.prototype.hasOwnProperty.call(obj, name);
var oldValue = obj[name];
var result;
try {
obj[name] = newValue;
@ -38,12 +41,16 @@ function isWritable(obj, name, verifyProp, value) {
}
}
if ((verifyProp && isEqualTo(obj, verifyProp, newValue)) ||
isEqualTo(obj, name, newValue)) {
return true;
result = (verifyProp && isEqualTo(obj, verifyProp, newValue)) ||
isEqualTo(obj, name, newValue);
if (hadValue) {
obj[name] = oldValue;
} else {
delete obj[name];
}
return false;
return result;
}
function verifyEqualTo(obj, name, value) {