Skip to content

Instantly share code, notes, and snippets.

@nikcorg
Created December 19, 2012 10:32
Show Gist options
  • Save nikcorg/4335816 to your computer and use it in GitHub Desktop.
Save nikcorg/4335816 to your computer and use it in GitHub Desktop.
Experimenting with prototype inheritance
var Foo = (function (Parent) {
var _super = Parent.prototype;
function Foo() {
Parent.call(this);
}
Foo.prototype = new Parent;
Foo.prototype.constructor = Foo;
Foo.prototype.ping = function () {
console.log('foo.ping', this);
};
return Foo;
}(Object));
var Bar = (function (Parent) {
var _super = Parent.prototype;
function Bar() {
Parent.call(this);
}
Bar.prototype = new Parent;
Bar.prototype.constructor = Bar;
Bar.prototype.ping = function () {
_super.ping.call(this);
console.log('bar.ping', this);
};
return Bar;
}(Foo));
var Baz = (function (Parent) {
var _super = Parent.prototype;
function Baz() {
Parent.call(this);
}
Baz.prototype = new Parent;
Baz.prototype.constructor = Baz;
Baz.prototype.ping = function () {
_super.ping.call(this);
console.log('baz.ping', this);
};
return Baz;
}(Bar));
var o = new Baz();
o.ping();
console.log("o instance of Baz", o instanceof Baz);
console.log("o instance of Bar", o instanceof Bar);
console.log("o instance of Foo", o instanceof Foo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment