I expect this results in console: "2" and "11". But instead it gives me "2" and "2". Why publicFunc2
is not overriden? http://jsfiddle.net/17b5ytmq/1/. I guess I don't understand something about subclassing in Javascript.
function MyClass() {
var self = this;
this._protectedArr = [1];
this.publicFunc = function() {
this._protectedArr.forEach(function(element, index, array) {
console.log(element + self.publicFunc2());
});
}
this.publicFunc2 = function () {
return 1;
}
}
function MyClassChild() {
this.publicFunc2 = function () {
return 10;
}
}
var myClass = new MyClass();
myClass.publicFunc();
MyClassChild.prototype = myClass;
var myClassChild = new MyClassChild()
myClassChild.publicFunc();