multiple constructor in javascript

javascript class constructor
javascript constructor
javascript constructor prototype
javascript prototype
javascript multiple constructors es6
w3schools constructor javascript
constructor pattern javascript
javascript class prototype

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)?

Let's say that I have a class called "Point" which would have two values "x" and "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:

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?

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

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
    }
}

constructor, Any JavaScript function can be used as a constructor. You use the Some object​-oriented languages allow multiple inheritance. That is, an  In JavaScript, the thing called this is the object that "owns" the code. The value of this, when used in an object, is the object itself. In a constructor function this does not have a value. It is a substitute for the new object. The value of this will become the new object when a new object is created.

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.

Java requires two different constructors because it is strongly typed and the argument types have to match the method signature, however this isn't the case with 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");
    }
};

Details of the object model, It is considered good practice to name constructor functions with an upper-case first letter. Object Types (Blueprints) (Classes). The examples from the previous  Multiple Constructors in Java with Example (Last Updated On: February 27, 2019) Learn multiple constructors in java with example. A class can have multiple constructors with different types of arguments and different number of arguments.

This is inspired from 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.

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

JavaScript Constructors, A constructor is useful when you want to create multiple similar objects with the same properties and methods. It's a convention to capitalize the name of  multiple constructors in JavaScript? What you want shall be called "polymorphic function-constructor". As you properly noticed, a polymorphic constructor (in any language) is in fact two or more constructors "hidden" under one common interface (explicit in JavaScript or implicit in Java). The rest gets simple: <script type="text/javascript">

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);
}

Understanding JavaScript Constructors, A class can have multiple constructors that assign the fields in different ways. Sometimes it's beneficial to specify every aspect of an object's data by assigning​  Constructor: A constructor is a function that initializes an object. In JavaScript the constructors are more similar to normal java constructor. Object constructor: In JavaScript, there is a special constructor function known as Object() is used to create and initialize an object.

Multiple constructors, If a class had multiple constructors, what would calling them look like? method overloading). but In JavaScript, you have only one constructor. There are two types of constructors: built-in constructors such as Array and Object, which are available automatically in the execution environment at runtime; and custom constructors, which define properties and methods for your own type of object. A constructor is useful when you want to create multiple similar

Can a class only have one constructor?, Are multiple "constructors" allowed in JavaScript?. JavaScript / Ajax / DHTML Forums on Bytes. If your class is a base class, the default constructor is empty: constructor() {} If your class is a derived class, the default constructor calls the parent constructor, passing along any arguments that were provided: constructor(args) { super(args); } That enables code like this to work:

Are multiple "constructors" allowed in JavaScript?, But often we need to create many similar objects, like multiple users or menu items and so on. That can be done using constructor functions  Constructor functions should only be called using new. Such a call implies a creation of empty this at the start and returning the populated one at the end. We can use constructor functions to make multiple similar objects. JavaScript provides constructor functions for many built-in language objects: like Date for dates, Set for sets and others

Comments
  • Not possible. You might find some things useful in the answers to this question though: stackoverflow.com/questions/456177/…
  • possible duplicate of JavaScript pattern for multiple constructors
  • Possible duplicate of javascript: different constructors for same type of object or (older) stackoverflow.com/questions/3220721/…
  • What is nice about Java is that you can have the same constructor name