javascript javascript中的多个构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7481988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:15:56  来源:igfitidea点击:

multiple constructor in javascript

javascript

提问by Arslan Ahson

I have a question: I was wondering if it is possible to simulate the multiple constructors, like in Java (yes, I know that the languages are completely different)?

我有一个问题:我想知道是否可以模拟多个构造函数,就像在 Java 中一样(是的,我知道这些语言完全不同)?

Let's say that I have a class called "Point" which would have two values "x" and "y".

假设我有一个名为“Point”的类,它有两个值“x”和“y”。

Now, let's say if it were the Java version, I would want two constructors: one that accept two numbers, the other accepts a string:

现在,假设它是 Java 版本,我想要两个构造函数:一个接受两个数字,另一个接受一个字符串:

public class Point {
    private int x;
    private int y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public Point(String coord) {
        this.x = coord.charAt(0);
        this.y = coord.charAt(1);
    }
    //...
}


//In JavaScript, so far I have
Point = function() {
    var x;
    var y;
    //...
}

Is it possible to have two declarations for the Point.prototype.init? Is it even possible to have multiple constructors in JavaScript?

Point.prototype.init 是否可以有两个声明?在 JavaScript 中甚至可以有多个构造函数吗?

回答by Arnaud Le Blanc

You can do this in javascript by testing the number of arguments, or the type of the arguments.

您可以在 javascript 中通过测试参数的数量或参数的类型来做到这一点。

In this case, you can do it by testing the number of arguments:

在这种情况下,您可以通过测试参数的数量来实现:

function Point(/* x,y | coord */) {
    if (arguments.length == 2) {
        var x = arguments[0];
        var y = arguments[1];
        // do something with x and y
    } else {
        var coord = arguments[0];
        // do something with coord
    }
}

回答by Matt

Yes, you can, although not as your expecting. As Javascript is weakly typed, no-one cares or checks what type the arguments that you provide are.

是的,你可以,虽然不像你期望的那样。由于 Javascript 是弱类型的,没有人关心或检查您提供的参数是什么类型。

Java requires two differentconstructors because it is strongly typed and the argument types have to match the method signature, however this isn't the case with JavaScript.

Java 需要两个不同的构造函数,因为它是强类型的,并且参数类型必须与方法签名匹配,但是 JavaScript 不是这种情况。

function Point(arg1, arg2) {
    if (typeof arg1 === "number" && typeof arg2 === "number") {
        // blah
    } else if (typeof arg1 === "string" && arguments.length == 1) {
        // blah
    } else {
        throw new Error("Invalid arguments");
    }
};

回答by Alston

This is inspired from iOS.

这是受 iOS 启发的。

class Point {
    constructor() {
        this.x = 0; // default value
        this.y = 0; // default value
    }
    static initWithCoor(coor) {
        let point = new Point();
        point.x = coor.x;
        point.y = coor.y;
        return point;            
    }
    static initWithXY(x,y) {
        let point = new Point();
        point.x = x;
        point.y = y;
        return point;            
    }
}

Just like that, you could have as many initializers as you want without writing lots of if-else.

就像那样,您可以拥有任意数量的初始值设定项,而无需编写大量 if-else。

let p1 = Point.initWithCoor({ x:10, y:20 });
let p2 = Point.initWithXY(10, 20);

回答by John Page

Just make one constructor wrap another:

只需让一个构造函数包装另一个:

function Point(x,y) { 
//make the point and do what the constructor is designed to do
}

function PointStr(str) { 
var xp = arguments[0]; 
var yp = arguments[1]; 
return new Point(xp, yp);
}