I'm practicing polymorphism in JavaScript (first time trying) based on different examples I've found online. I know that in other languages I can access the variables of the super class from the extended one and am wondering how to do this correctly in JavaScript. The code below doesn't throw any error (at least as far as Firefox's Error Console is concerned), but statement is undefined in ExtendedClass.
function MyClass() {
this.statement = "I'm a class with a method";
this.speak = function() {
alert(this.statement);
}
}
var mInstance = new MyClass();
mInstance.speak();
function ExtendedClass() {
Object.create(MyClass);
this.speak = function() {
alert(this.statement+" and I extend a class");
}
}
var eInstance = new ExtendedClass();
eInstance.speak();
Can I access statement from ExtendedClass? Is this a good method of implementing polymorphism?