Skip to content
Snippets Groups Projects
Commit c27938a1 authored by Mike Pennisi's avatar Mike Pennisi
Browse files

Fix bug in `isWritable` utility function

Only attempt to re-set the property value in cases where it was
successfully modified as part of the function's execution. This avoids
errors when the underlying value is not writable. Rename the internal
result-tracking variable to make this more clear.
parent e9593a3d
Branches
No related tags found
No related merge requests found
......@@ -31,7 +31,7 @@ function isWritable(obj, name, verifyProp, value) {
var newValue = value || "unlikelyValue";
var hadValue = Object.prototype.hasOwnProperty.call(obj, name);
var oldValue = obj[name];
var result;
var writeSucceeded;
try {
obj[name] = newValue;
......@@ -41,16 +41,21 @@ function isWritable(obj, name, verifyProp, value) {
}
}
result = (verifyProp && isEqualTo(obj, verifyProp, newValue)) ||
writeSucceeded = (verifyProp && isEqualTo(obj, verifyProp, newValue)) ||
isEqualTo(obj, name, newValue);
if (hadValue) {
obj[name] = oldValue;
} else {
delete obj[name];
// Revert the change only if it was successful (in other cases, reverting
// is unnecessary and may trigger exceptions for certain property
// configurations)
if (writeSucceeded) {
if (hadValue) {
obj[name] = oldValue;
} else {
delete obj[name];
}
}
return result;
return writeSucceeded;
}
function verifyEqualTo(obj, name, value) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment