Skip to content

Instantly share code, notes, and snippets.

@robdodson
Created August 9, 2012 18:00
Show Gist options
  • Select an option

  • Save robdodson/3306571 to your computer and use it in GitHub Desktop.

Select an option

Save robdodson/3306571 to your computer and use it in GitHub Desktop.
inherit method
// Optimized version of classical inheritance
// Proxy constructor is stored in a closure which is
// returned from a self executing function.
var inherit = (function() {
var F = function() {};
return function(C, P) {
F.prototype = P.prototype;
C.prototype = new F();
C._super = P.prototype;
C.prototype.constructor = C;
}
})();
function Parent(a, b) {
this.name = 'Adam';
}
Parent.prototype.say = function() {
console.log(this.name);
}
function Child() {}
inherit(Child, Parent); // Must be called before manipulating Child's prototype
Child.prototype.newShiz = function() {
console.log('that new shit!');
}
var child = new Child();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment