Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Created August 27, 2008 13:20
Show Gist options
  • Save jcoglan/7477 to your computer and use it in GitHub Desktop.
Save jcoglan/7477 to your computer and use it in GitHub Desktop.
// 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