Created
August 27, 2008 13:20
-
-
Save jcoglan/7477 to your computer and use it in GitHub Desktop.
This file contains 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
// Turns any function into a constructor that will work | |
// with or without the 'new' keyword. The returned function | |
// shares its prototype with the source function for | |
// transparency. | |
Function.prototype.toClass = function() { | |
var func = this, bridge = function() {}; | |
bridge.prototype = func.prototype; | |
var make = function(args) { | |
var object = new bridge; | |
func.apply(object, args); | |
return object; | |
}; | |
var factory = function() { | |
return make(arguments); | |
}; | |
factory.prototype = func.prototype; | |
return factory; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment