Skip to content

Instantly share code, notes, and snippets.

@psdcoder
Created August 27, 2014 20:37
Show Gist options
  • Save psdcoder/212b7e8d4827fa56b07f to your computer and use it in GitHub Desktop.
Save psdcoder/212b7e8d4827fa56b07f to your computer and use it in GitHub Desktop.
node js inheritance of classes
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