Skip to content

Instantly share code, notes, and snippets.

@polygonplanet
Last active December 29, 2015 20:19
Show Gist options
  • Save polygonplanet/7723007 to your computer and use it in GitHub Desktop.
Save polygonplanet/7723007 to your computer and use it in GitHub Desktop.
ECMAScript6 - Proxy - default handler
// Proxy default handler
//
// Proxy.Handler.prototype
// http://wiki.ecmascript.org/doku.php?id=harmony:proxy_defaulthandler
// http://wiki.ecmascript.org/doku.php?id=harmony:proxies
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
var getDefaultProxyHandler = function getDefaultProxyHandler() {
return {
// == fundamental traps ==
// Object.getOwnPropertyDescriptor(proxy, name) -> PropertyDescriptor | undefined
getOwnPropertyDescriptor: function(target, name) {
var desc = Object.getOwnPropertyDescriptor(target, name);
desc && (desc.configurable = true);
return desc;
},
// Object.getPropertyDescriptor(proxy, name) -> PropertyDescriptor | undefined
getPropertyDescriptor: function(target, name) {
var desc = Object.getPropertyDescriptor(target, name);
desc && (desc.configurable = true);
return desc;
},
// Object.getOwnPropertyNames(proxy) -> [String]
getOwnPropertyNames: function(target) {
return Object.getOwnPropertyNames(target);
},
// Object.getPropertyNames(proxy) -> [String]
getPropertyNames: function(target) {
return Object.getPropertyNames(target);
},
// Object.defineProperty(proxy, name, pd) -> undefined | any
defineProperty: function(target, name, desc) {
return Object.defineProperty(target, name, desc);
},
// delete proxy[name] -> boolean
delete: function(target, name) {
return delete target[name];
},
// Object.freeze(proxy) -> boolean
freeze: function(target) {
//FIXME: return boolean?
return Object.freeze(target);
},
// Object.seal(proxy) -> boolean
seal: function(target) {
//FIXME: return value
return Object.seal(target);
},
// Object.preventExtensions(proxy) -> boolean
preventExtensions: function(target) {
return Object.preventExtensions(target);
},
// Object.{freeze|seal|preventExtensions}(proxy) -> proxy
fix: function(target) {
if (Object.isFrozen(target)) {
var props = {};
Object.getOwnPropertyNames(target).forEach(function(name) {
props[name] = Object.getOwnPropertyDescriptor(target, name);
});
return props;
}
return void 0;
},
// == derived traps ==
// name in proxy -> boolean
has: function(target, name) {
return name in target;
},
// Object.prototype.hasOwnProperty.call(proxy, name) -> boolean
hasOwn: function(target, name) {
return Object.prototype.hasOwnProperty.call(target, name);
},
// proxy[name] -> any
get: function(target, name, receiver) {
return target[name];
},
// proxy[name] = value -> boolean
set: function(target, name, value, receiver) {
// canPut as defined in ES5 8.12.4 [[CanPut]]
//if (typeof canPut === 'undefined' || canPut(target, name)) {
target[name] = value;
return true;
//}
// causes proxy to throw in strict mode, ignore otherwise
//return false;
},
// for (var prop in proxy) { ... } -> [String]
//FIXME: Bug 783829 - Using for-in on a proxy should
// call the enumerate trap, not the iterate trap
// https://bugzilla.mozilla.org/show_bug.cgi?id=783829
enumerate: function(target) {
var keys = {};
for (var key in target) {
keys[key] = null;
}
return Object.keys(keys);
},
// for (var prop of proxy) { ... } -> iterator
iterate: function(target) {
//FIXME: Plain object is not iterable.
for (var key in target) {
yield [key, target[key]];
}
},
// Object.keys(proxy) -> [String]
keys: function(target) {
return Object.keys(target);
},
// == functional traps ==
//FIXME: Below is valid only when used Proxy.createFunction?
// And should fix function code.
// proxy.apply(thisValue, args) -> any
apply: function(target, thisValue, args) {
if (typeof target.apply === 'function') {
return target.apply(thisValue, args);
}
},
// new proxy(...args) -> any
construct: function(target, args) {
if (typeof target === 'function') {
return new (target.bind.apply(target, args));
}
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment