Skip to content

Instantly share code, notes, and snippets.

@therightstuff
Created September 11, 2018 22:56
Show Gist options
  • Save therightstuff/1a69c292ecc38f434cf0e63b4983e9c7 to your computer and use it in GitHub Desktop.
Save therightstuff/1a69c292ecc38f434cf0e63b4983e9c7 to your computer and use it in GitHub Desktop.
simple node.js inheritance example
var util = require("util");
// simple class
function A() {
this.self= this;
this.className = "classA";
this.myVar = "ownerA";
}
// class methods
A.prototype.getMyVar = function() {
return this.myVar;
};
// inherited class with parameters
function B(classNameOverride) {
A.call(this);
this.self.className = classNameOverride || "classB";
}
util.inherits(B, A);
// inherited class using super class parameters
function C() {
B.call(this, "classC");
}
util.inherits(C, B);
// method override
C.prototype.getMyVar = function() {
return "ownerC";
};
var a = new A();
var b = new B();
var c = new C();
console.log(a.className + ' ' + b.className + ' ' + c.className);
console.log(a.getMyVar() + ' ' + b.getMyVar() + ' ' + c.getMyVar());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment