Skip to content

Instantly share code, notes, and snippets.

@RacingTadpole
Forked from coolaj86/new_constructor.js
Last active August 29, 2015 14:07
Show Gist options
  • Save RacingTadpole/59b22af8e2b5462ddb25 to your computer and use it in GitHub Desktop.
Save RacingTadpole/59b22af8e2b5462ddb25 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