Created
April 26, 2012 18:22
-
-
Save neonux/2501635 to your computer and use it in GitHub Desktop.
read-only proxy
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 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