3

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?

3 Answers 3

2

You can use MyClass.call(this) to set the local variables, and then use Object.create to set the prototype, like this

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() {
  MyClass.call(this);
  this.speak = function() {
    alert(this.statement+" and I extend a class");
  }
}
ExtendedClass.prototype = Object.create(MyClass.prototype);
var eInstance = new ExtendedClass();
eInstance.speak();

You can see more at the MDN Docs

Sign up to request clarification or add additional context in comments.

5 Comments

This looks just like RyanFishman's answer but has ExtendedClass.prototype = Object.create(MyClass.prototype); in it as well, what does that do? I'm not used to using prototype or Object.create()
yep, we posted simultaneously. This post may help give a long explanation: benmccormick.org/blog/2013/01/12/…, but the short explanation is that Javascript inheritance works differently than classical inheritance. Objects have a "prototype chain" that they can share with other objects. All Objects share the Object.prototype prototype by default, and they can also share other object's prototypes, as shown above. The MDN article I linked also explains this.
Ah, my apologies, I should have worded that differently, I didn't mean to suggest you wrote your answer after seeing his. Thank you
Just double checking, the extra line is for correcting ExtendedClass's constructor pointer?
Not exactly. the function ExtendedClass() is the constructor for the ExtendedClass type. ExtendedCLass.prototype is a reference to the ExtendedClass type's prototype chain, which is how inheritance is controlled in javascript. It doesn't have an exact equivalent in languages like javascript or C++ because those languages use classical inheritance rather than prototypical inheritance.
1

I do it like this

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() {
  MyClass.call(this);
  this.speak = function() {
    alert(this.statement+" and I extend a class");
  }
}
var eInstance = new ExtendedClass();
eInstance.speak();

Not sure if this is the best syntax but I know this works and you properly inherit MyClass with all it's public methods and variables.

Comments

1

Prototypes are what you're looking for.

var MyClass = function(){}; //Empty Constructor
MyClass.prototype = {
    statement: "I'm a class with a method",
    speak: function(){
        alert(this.statement);
    }
};
var ExtendedClass = function(){}; //Empty Constructor
ExtendedClass.prototype = new MyClass();
ExtendedClass.prototype.speak = function(){
    alert(this.statement+" and I extend a class");
};
var eInstance = new ExtendedClass();
eInstance.speak();

6 Comments

Is this a different way to create ExtendedClass? The syntax doesn't look like a class declaration from the languages I know
Yeah, Javascript's OOP and inheritance styles are non-traditional (to put it kindly). This is about as condensed as I can get it without using the call method. Not sure why, I just never liked using methods to actually inherit properties and methods from other objects. It all boils down to what your personal preference is, and how readable it is to you.
I just tried copy / pasting this to try it since it looks so different from the other ones but it throws an error: MyClass is not defined
My apologies. I forgot to define the actual Objects in the original code. Updated with a tested working example.
Thanks for fixing it. I think I'm going to stick with the ones that look like Java for the time being, but it is good to see more examples
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.