Skip to content

Instantly share code, notes, and snippets.

@tomas-stefano
Last active August 29, 2015 14:00
Show Gist options
  • Save tomas-stefano/11354072 to your computer and use it in GitHub Desktop.
Save tomas-stefano/11354072 to your computer and use it in GitHub Desktop.
Inheritance Javascript from scratch with super call
var Bar, Foo,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) {
for (var key in parent) {
if (__hasProp.call(parent, key)) child[key] = parent[key];
}
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.__super__ = parent.prototype;
return child;
};
Foo = (function() {
function Foo() {
this.foo = 'foo';
}
return Foo;
})();
Bar = (function(_super) {
__extends(Bar, _super);
function Bar() {
this.bar = 'bar';
Bar.__super__.constructor.apply(this, arguments);
}
return Bar;
})(Foo);
var foo = new Foo();
foo.foo;
var bar = new Bar();
bar.foo;
bar.bar;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment