Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Constructor Function Proxies
/**
* A convenience method to create a proper Constructor Function
* implemented as a function proxy
*/
Proxy.createConstructor = function(handler, callTrap) {
var proto = new Object();
var ctor = Proxy.createFunction(
handler,
callTrap,
function() {
var instance = Object.create(proto);
var result = // callTrap.apply(instance, arguments);
Function.prototype.apply.call(callTrap, instance, arguments);
if (Object(result) === result) {
return result;
} else {
return instance;
}
});
Object.defineProperty(proto, "constructor", {
value: ctor,
writable: true,
enumerable: false,
configurable: true });
Object.defineProperty(ctor, "prototype", {
value: proto,
writable: true,
enumerable: false,
configurable: false });
Object.defineProperty(ctor, "length", {
value: callTrap.length,
writable: false,
enumerable: false,
configurable: false });
return ctor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment