Skip to content
Advertisement

JavaScript: ‘this’ in superclass refers to subclass

I am calling a superclass method from a subclass. When called from a subclass, ‘this’ in the superclass method no longer refers to the superclass but to the subclass that called it. This creates problems with inheritance.

Details as follows:

In my project I use a class to construct objects from registered types. The types are registered from properties, or alternatively from a template based on an already defined type:

JavaScript

This works fine in the superclass. However, I have also constructed a subclass with more user-friendly methods. In the subclass, the method called ‘createObject’ doesn’t return the created object. It stores it and returns the object id:

JavaScript

When I now try to create template-based objects from the subclass, and the superclass code comes to this point:

JavaScript

the ‘this.createObject(…)’ call results in calling the subclass’ createObject-method, thus returning a numerical key, and failing on trying to assign the type to it.

I know I could for instance rename the method in the subclass to avoid the problem, I could send in the class object (the ‘this’) as a parameter or other tricky stuff.

But is there a more straightforward, idiomatic way to solve this?

Advertisement

Answer

this refers to an object, not to a class. In this case, it’s an instance of B in normal use (even in code in A — there’s a single object). Since the createObject property of the object comes from B, that’s what you get from this.createObject (regardless of where that code occurs).

But there’s a more fundamental issue:

When you subclass something, you’re saying that it is a more specialized version of the thing you subclassed. That is, B is a specialised A. That means B is an A. A has a particular contract, e.g. it returns an object from its createObject method. By changing that return type in B, you’re breaking that contract. B is no longer an A, it’s something A-like except for createObject.

So I recommend not doing that, since it breaks one of the rules of subclassing.

You can call A‘s version of createObject from the code in A, like this:

JavaScript

but I strongly recommend that you don’t. 🙂 Instead, don’t make B break A‘s contract. Use a different method for what you’re doing in B.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement