0

I am new to JavaScript, I have been working in Java and C++, there we have polymorphism for Constructors; Example:

class Car
{
     Car()
     {
         ....
         //body
         ....
     }
     Car(int speed, string body_type)
     {
          ....
          //body
          ....
     }
};

I was trying object oriented programming in JavaScript using the same concept. I wrote this code:

<html>
<head>
    <script type="text/javascript">
    function bike()
    {
        this.bike_type="scooter";
    this.bike_bidy="metal";
        this.bike_speed=40;
        document.write('bike instantiated with type:'+this.bike_type);  
    }
    function bike(type, body, speed)
    {
        this.bike_type=type;
        this.bike_bidy=body;
        this.bike_speed=speed;
        document.write('bike instantiated with type:'+type);    
    }
    bike1 = new bike("harley","metal",120);
    bike2 = new bike();
    document.write(bike1.bike_type);
    document.write(bike2.bike_speed);
</script>
</head>
<body>
</body>
</html>

But here, I get instance only for the bike() function/class not for bike(type, body, speed). When I tried this:

<html>
<head>
    <script type="text/javascript">
    function bike(type, body, speed)
    {
        this.bike_type=type;
        this.bike_bidy=body;
        this.bike_speed=speed;
        document.write('bike instantiated with type:'+type);    
    }
    function bike()
    {
        this.bike_type="scooter";
    this.bike_bidy="metal";
        this.bike_speed=40;
        document.write('bike instantiated with type:'+this.bike_type);  
    }
    bike1 = new bike("harley","metal",120);
    bike2 = new bike();
    document.write(bike1.bike_type);
    document.write(bike2.bike_speed);
</script>
</head>
<body>
</body>
</html>

Then I get, instance for function bike(type, body, speed) not for bike(). I understood that it is only taking the 1st definition. And the way I am doing is not allowed.

How can I make a polymorphic constructor in JavaScript? Is it possible?

1
  • 2
    CAPS AND BOLD EQUALS SHOUTING. Never appropriate on Stack Overflow. Commented Nov 19, 2013 at 6:52

2 Answers 2

4

In Javascript:

  • a global function can only be defined once
  • function arguments are optional
  • function arguments are available through a pseudo-array called arguments (see the docs).

So you can do something like this:

function bike(type, body, speed)
{
    if (arguments.length >= 3) {
        this.bike_type = type;
        this.bike_body = body;
        this.bike_speed = speed;
        document.write('bike instantiated with type:'+type);    
    } else {
        this.bike_type = "scooter";
        this.bike_body = "metal";
        this.bike_speed = 40;
        document.write('bike instantiated with type:'+this.bike_type);  
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

No, it's not possible. In both cases, the second function definition overwrites the first.

You have to examine your arguments from within your function, and alter your functions behaviour accordingly.

In your case, you can simply use defaults if none were given:

function bike(type, body, speed)
{
    this.bike_type = type || "scooter";
    this.bike_bidy = body || "metal";
    this.bike_speed = speed || 40;
}

It is also very common to use a single options argument, something like bike({body: "carbon fiber"}), allowing you to omit leading parameters without having to specify undefined in their place.

function bike(options)
{
    this.bike_type = options.type || "scooter";
    // ...
}

1 Comment

That will cause problems with any passed-in arguments which evaluate to false - for instance, bike("broken", "metal", 0) will give you a bike with speed of 40.

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.