Skip to content

Instantly share code, notes, and snippets.

@tvcutsem
Created August 31, 2011 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvcutsem/1183514 to your computer and use it in GitHub Desktop.
Save tvcutsem/1183514 to your computer and use it in GitHub Desktop.
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