Home > database >  Why doesn't the following code throw an error when we create a new instance?
Why doesn't the following code throw an error when we create a new instance?

Time:08-12

class Person {}

let agent = new Person(50);
console.log(agent);

We haven't defined a constructor method on the Person class nor does the Person class inherit (extend) from an another class which has a constructor method.

CodePudding user response:

All classes in javascript have a default constructor method if the programmer does not define one. Try this in inspector:

class Person {}
console.log(Person);

and you'll be able to see the default constructor Javascript applies.

  • Related