简体   繁体   English

JavaScript中的多态具有不同的构造函数

[英]Polymorphism in JavaScript to have different constructors

I am new to JavaScript, I have been working in Java and C++, there we have polymorphism for Constructors; 我是JavaScript的新手,我一直在Java和C ++中工作,那里构造函数具有多态性。 Example: 例:

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

I was trying object oriented programming in JavaScript using the same concept. 我正在尝试使用相同的概念在JavaScript中进行面向对象的编程。 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) . 但是在这里,我仅获得bike() 函数/类的实例,而没有获得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() . 然后我得到了function bike(type, body, speed)实例,而不是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? 如何在JavaScript中创建多态构造函数? Is it possible? 可能吗?

In Javascript: 用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 ). 函数参数可通过称为arguments的伪数组获得(请参阅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);  
    }
}

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. 使用单个选项参数也是很常见的,例如bike({body: "carbon fiber"}) ,允许您省略前导参数而不必在其位置指定undefined

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM