Maybe someone can explain to me, why I can’t override the method moep from B’s prototype-class. I’ve found an example (http://stackoverflow.com/questions/11148960/javascript-prototype-method-override-not-found) and if I’m overriding the function with B.prototype = … it works. So why do I have to specify the .prototype to override the function?
Greetings – Thomas
A = function() {
this.moep = function() {
alert("Im in class A!");
};
};
B = function() {
};
B.prototype = new A();
B.moep = function() {
alert("Im outside!");
};
var keks = new B();
keks.moep(); // Alerts "Im in class A"
You’re assigning to
B.moep, notB.prototype.moepor (withinB)this.moep.B.moepisn’t involved in the prototype chain at all.When you create objects via
new <functionname>, the object’s prototype is set from<functionname>.prototype. So if you want to override themoepassigned byAto the instance created bynew Aand assigned toB.prototype, you need to assign toB.prototype.