Created
August 27, 2014 20:37
-
-
Save psdcoder/212b7e8d4827fa56b07f to your computer and use it in GitHub Desktop.
node js inheritance of classes
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'); | |
function BaseClass(data) { | |
if (!data) { | |
throw new TypeError('You must pass "data" in constructor'); | |
} | |
console.log('base constructor'); | |
this.data = data; | |
} | |
BaseClass.prototype.getData = function () { | |
console.log('get data from the base class'); | |
return this.data; | |
}; | |
//Child | |
function ChildClass(data) { | |
ChildClass.super_.apply(this, Array.prototype.slice.call(arguments)); | |
} | |
util.inherits(ChildClass, BaseClass); | |
ChildClass.prototype.getData = function () { | |
console.log('get data from the child class'); | |
return ChildClass.super_.prototype.getData.call(this); | |
}; | |
var child = new ChildClass('test data'); | |
console.log(child.getData()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment