Skip to content

Instantly share code, notes, and snippets.

@neonux
Created April 26, 2012 18:22
Show Gist options
  • Save neonux/2501635 to your computer and use it in GitHub Desktop.
Save neonux/2501635 to your computer and use it in GitHub Desktop.
read-only proxy
function wrap(aObject)
{
if (aObject === undefined || aObject === null) {
return aObject;
}
let type = typeof(aObject);
if (type == "string" || type == "number" || type == "boolean" || type == "function") {
return aObject;
}
let getOwnPropertyDescriptor = function (aObject, name) {
var desc = Object.getOwnPropertyDescriptor(aObject, name);
if (desc === undefined) {
return undefined;
}
if ("value" in desc) {
desc.value = wrap(desc.value);
desc.writable = false;
} else {
desc.getter = wrap(desc.getter);
desc.setter = null;
}
return desc;
};
let getPropertyDescriptor = function (aObject, name) {
let desc;
while (aObject && undefined === (desc = getOwnPropertyDescriptor(aObject, name))) {
aObject = Object.getPrototypeOf(aObject);
}
return desc;
};
return Proxy.create({
getOwnPropertyDescriptor: function (name) {
return getOwnPropertyDescriptor(aObject, name);
},
getPropertyDescriptor: function (name) {
return getPropertyDescriptor(aObject, name);
}
}, wrap(Object.getPrototypeOf(aObject)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment