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

Viewed 42
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.

1 Answers

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