I'm trying to extend class B to class A, and class A to class Super, using the function apply. The following code works OK:
function Super() {
this.talk = function () {
alert("Hello");
};
}
function A() {
// instance of A inherits Super
Super.apply(this);
}
function B() {
// instance of B inherits A
A.apply(this);
}
var x = new B();
x.talk(); // Hello
But what if I want to class A inherits from class Super, not just its instances? I tried this:
function Super() {
this.talk = function () {
alert("Hello, I'm the class");
};
// function of the class' instance?
this.prototype.talk = function () {
alert("Hello, I'm the object");
};
}
function A() {
// nothing here
}
// A inherits from Super, not its instance
Super.apply(A);
function B() {
// instance of B inherits A
A.apply(this);
}
A.talk(); // static function works!
var x = new B();
x.talk(); // but this doesn't...
Am I doing something wrong?
talkfunction outside ofSuperand specify it like:Super.prototype.talk = function() { // Stuff }?B.prototype = Object.create(A.prototype);after declaring functionBand aboveA.talk();and it should work. This is explained below in detail.