-1
class Camera {
    constructor(id){
        this.id = id;
    }

    constructor(id, name){
        this.id = id;
        this.name = name;
    }
}

let camera = new Camera('A456','Karan');
let drone = new Camera('A1');


console.log(`camera: ${camera['id']} ${camera['name']}`)
console.log(`drone: ${drone['id']}`)



**IS the ABOVE code said as the constructor overloading?**

I am getting this code as success output but when i change the sequence of constructors , i am getting an error

Karan KJ
  • 131
  • 1
  • 8
  • 2
    No. Function and method overloading do not exist. – Andrew Li Dec 18 '18 at 04:28
  • As Javascript is able to allow parameters to be ignored. ie funcX(var1, var2) can be validly called using funcX() you effectively have overloading. You would need to protect and initialize variables not passed tho. – Bibberty Dec 18 '18 at 04:36
  • I have edited the code , please check and give a clarified answer – Karan KJ Dec 18 '18 at 04:55

2 Answers2

0

// Our object declares all the properties it natively supports.
function Person(name, age, location) {
  this.name = name;
  this.age = age;
  this.location = location;

  // Deal with essential properties
  if(this.name === undefined) this.name = 'John Doe';
};

var paul = new Person("Paul");

var person = new Person();

console.log(paul);
console.log(person);
Bibberty
  • 4,670
  • 2
  • 8
  • 23
-2

Yes JavaScript supports the constructor overloading concept but Partially. It works according to bottom up Approach so it will work according to sequence of constructors.

The Code below will run according to the bottom up approach and will execute the output according to it.

class Camera {
    constructor(id){
        this.id = id;
    }

    constructor(id, name){
        this.id = id;
        this.name = name;
    }
}

let camera = new Camera('A456','Karan');
let drone = new Camera('A1');


console.log(`camera: ${camera['id']} ${camera['name']}`)
console.log(`drone: ${drone['id']}`)
Karan KJ
  • 131
  • 1
  • 8