Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7701407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:58:41+00:00 2026-05-31T22:58:41+00:00

I am a programmer who has programmed in several languages, both functional and OO

  • 0

I am a programmer who has programmed in several languages, both functional and OO oriented. I programmed some Javascript too, but never used (or had to use) polymorphism in it.

Now, as kind of a hobby project, I would like to port some apps that were written in Java and C# that heavily use polymorhpism to Javascript.

But at a first glance I read lots of ‘It’s possible but …’

So is there an alternative to it?

An example of what I would like to write in JS in pseudolang :

abstract class Shape{ { printSurface() } ;  }

class Rect : Shape() {  printSurface() { print (sideA*sideB}}

class Circle : Shape() {  printSurface() { print { pi*r*r }}

TheApp { myshapes.iterate(shape s) {s.printSurface() } }

So a classic polymorphic case : iterating over baseclass.

I would like to achieve this kind of behaviour. I know it is polymorhism, but are there any other ‘patterns’ that I am overlooking that achieve this kind of behaviour or should I study the inheritance possibilities in Javascript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:58:43+00:00Added an answer on May 31, 2026 at 10:58 pm

    As said, JavaScript is a dynamic typed language based on prototypal inheritance, so you can’t really use the same approach of typed languages. A JS version of what you wrote, could be:

    function Shape(){
      throw new Error("Abstract class")
    }
    
    Shape.prototype.printSurface = function () {
      throw new Error ("Not implemented");
    }
    
    function Rect() {
      // constructor;
    }
    
    Rect.prototype = Object.create(Shape.prototype);
    Rect.prototype.printSurface = function() {
      // Rect implementation
    }
    
    function Circle() {
      // constructor;
    }
    
    Circle.prototype = Object.create(Shape.prototype);
    Circle.prototype.printSurface = function() {
      // Circle implementation
    }
    

    Then, in your app:

    var obj = new Circle();
    
    if (obj instanceof Shape) {
        // do something with a shape object
    }
    

    Or, with duck typing:

    if ("printSurface" in obj)
        obj.printSurface();
    
    // or
    
    if (obj.printSurface)
        obj.printSurface();
    
    // or a more specific check
    if (typeof obj.printSurface === "function")
        obj.printSurface();
    

    You cold also have Shape as object without any constructor, that it’s more “abstract class” like:

    var Shape = {
        printSurface : function () {
            throw new Error ("Not implemented");
        }
    }
    
    function Rect() {
      // constructor;
    }
    
    Rect.prototype = Object.create(Shape);
    Rect.prototype.printSurface = function() {
      // Rect implementation
    }
    
    function Circle() {
      // constructor;
    }
    
    Circle.prototype = Object.create(Shape);
    Circle.prototype.printSurface = function() {
      // Circle implementation
    }
    

    Notice that in this case, you can’t use instanceof anymore, so or you fallback to duck typing or you have to use isPrototypeOf, but is available only in recent browsers:

    if (Shape.isPrototypeOf(obj)) {
        // do something with obj
    }
    

    Object.create is not available in browser that doesn’t implement ES5 specs, but you can easily use a polyfill (see the link).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a programmer who has never really used .dll files. Of cause, when I
I'm a hobbyist (and fairly new) programmer who has written several useful (to me)
I am an experienced C/C++/C# programmer who has just gotten into VB.NET. I generally
I programmed a CMS that has a log of who has recently logged into
I'm a flex programmer who has just started to learn android today, successfully starting
I am a java programmer who has started to make apps for iPhone. I
I'm a C++ developer who has primarily programmed on Solaris and Linux until recently,
Anyone who has programmed with actionscript 3.0 has most certainly noticed its lack of
I believe any programmer who has been dealing with database requests in a gui
I am a Ruby programmer who has ended up developing a code generate for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.