Object Oriented JavaScript – polymorphism

In previous post about object-oriented programming in JavaScript, we described how inheritance works. In this post, I’ll cover one more important concept of object oriented programming – polymorphism. I will not go into theoretical details, since there many online resources available, such as this Wikipedia page. Rather, I will try to explain how polymorphism can be implemented in JavaScript. As usual, complete source code can be found on Github.

Inheritance-based polymorphism

This is the common type of polymorphism which can be found in class-based languages. It requires an object hierarchy, in which subclasses can override method definitions from superclass. That way, when ever parent class is expected as a parameter, we can substitute it with  it’s subclass. This is partly described in the previous blog post about inheritance. In this code snippet, inheritance-based polymorphism in JavaScript is shown:

In this sample, we create base class Base  and two subclasses, Sub1  and Sub2 . We also create a test  function, which accepts an array of Base  type. But, we can replace Base  type with any object of it’s subtypes, thus creating different behavior for greet  function. When we call test function with array of created objects, each object will print out different message based on it’s own definition of  greet function .

Duck typing

Another way to achieve polymorphism in JavaScript is through duck typing. Duck typing is a concept in which type checking is deferred at runtime, instead of compile time. It does not require that object follow specific inheritance hierarchy, but instead expects that objects contain specific methods and properties that will be used at runtime. Consider the following code snippet:

In this snippet, we create two objects which are in no way related, other then having same method sum , which takes two arguments. This method is implemented differently in each object, one returns a sum of arguments and the other returns product (not really good function name for product, but it’s good to show a point 🙂 ).  Now, in another piece of code, we can use these objects this way:

Here, we create two objects and a function duckTest . which expects two arguments. Notice that this function does not care about the type of arguments, only that each contains a method sum  which takes two numbers as arguments. By having different types of objects implement this same method, we can have many different behaviors, thus having polymorphic capabilities. Since JavaScript is dynamically types language, this type of polymorphism is trivial to implement.

Which one to use?

There’s no clear answer here, the best one would be “use which ever is appropriate for the specific use case”.  For example, you might use inheritance based polymorphism if you have complete control over all types of objects in the system, while duck typing might be more useful when working with third-party libraries. I would love to hear some opinions on each one, so if you have some thoughts on which one is more appropriate, please feel free to comment on this post.

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *