Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Created April 13, 2010 00:24
Show Gist options
  • Save coolaj86/364168 to your computer and use it in GitHub Desktop.
Save coolaj86/364168 to your computer and use it in GitHub Desktop.
// Crockford's new_constructor
// Act III: Function the Ultimate
// yuiblog.com/crockford
function new_constructor(extend, initializer, methods) {
var func, prototype = Object.create(extend &&
extend.prototype);
if (methods) {
methods.keys().forEach(function (key) {
prototype[key] = methods[key];
});
}
func = function () {
var that = Object.create(prototype);
if (typeof initializer === 'function') {
initializer.apply(that, arguments);
}
return that;
};
func.prototype= prototype;
prototype.constructor= func;
return func;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment