Skip to content

Instantly share code, notes, and snippets.

@kof
Created March 24, 2011 13:10
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kof/885027 to your computer and use it in GitHub Desktop.
Save kof/885027 to your computer and use it in GitHub Desktop.
nodejs like utility method for inheritance
/**
* Inherit prototype properties
* @param {Function} ctor
* @param {Function} superCtor
*/
_.mixin({
inherits: (function(){
function noop(){}
function ecma3(ctor, superCtor) {
noop.prototype = superCtor.prototype;
ctor.prototype = new noop;
ctor.prototype.constructor = superCtor;
}
function ecma5(ctor, superCtor) {
ctor.prototype = Object.create(superCtor.prototype, {
constructor: { value: ctor, enumerable: false }
});
}
return Object.create ? ecma5 : ecma3;
}())
});
@prust
Copy link

prust commented Oct 2, 2014

FYI, there's a bug here (found by @Suor in CSNW/sql-bricks#54). Line 13 should be ctor.prototype.constructor = ctor; This is fixed on my fork (I also added super_ and included a performance improvement from nodejs/node-v0.x-archive@1ba2c321)

@prust
Copy link

prust commented Oct 9, 2014

I just discovered https://github.com/isaacs/inherits, which has an identical implementation to my fork of this gist but should be preferred because it is in a github repo, is maintained and is used by browserify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment