Basic JS Inheritance
by mslocum
JavaScript
function first(val) {
this.val = val;
}
first.prototype.getVal = function() {
return this.val;
};
function second(val) {
first.call(this, val);
}
second.prototype = Object.create(first.prototype);
second.prototype.addOne = function() {
this.val++;
}
function third(val) {
second.call(this, val);
}
third.prototype = Object.create(second.prototype);
third.prototype.subOne = function() {
this.val--;
}
var a = new first(1);
var b = new second(2);
var c = new third(3);
alert(c.getVal());
c.addOne();
alert(c.getVal());