Created
September 11, 2018 22:56
-
-
Save therightstuff/1a69c292ecc38f434cf0e63b4983e9c7 to your computer and use it in GitHub Desktop.
simple node.js inheritance example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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