Created
August 31, 2011 13:15
-
-
Save tvcutsem/1183514 to your computer and use it in GitHub Desktop.
Constructor Function Proxies
This file contains hidden or 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
/** | |
* 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