Skip to content

Instantly share code, notes, and snippets.

@semlinker
Created October 18, 2022 10:48
Show Gist options
  • Save semlinker/15511bb4450f41fc54b21858fa87f970 to your computer and use it in GitHub Desktop.
Save semlinker/15511bb4450f41fc54b21858fa87f970 to your computer and use it in GitHub Desktop.
Proxy API usage scenarios —— Sandbox
function sandbox(code) {
code = "with (sandbox) {" + code + "}";
const fn = new Function("sandbox", code);
return function (sandbox) {
const sandboxProxy = new Proxy(sandbox, {
has(target, key) {
return true;
},
get(target, key) {
if (key === Symbol.unscopables) return undefined;
return target[key];
},
});
return fn(sandboxProxy);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment