Kevin Powell - CommunityKP-C
Kevin Powell - Communityβ€’3y agoβ€’
35 replies
Γ… Marlon G

Constructor confusion ... JS Newbie

In codecademys JS course, a class is defined as an object blueprint, and to call it you must add the constructor function (constructor()):
class Dog {
  constructor(name) {
    this.name = name;
    this.behavior = 0;
  }
}

To create an instance, you then use the new keyword ...
const halley = new Dog('Halley');


But on javascript.info this is the definition of a constructor function:
function User(name) {
  this.name = name;
  this.isAdmin = false;
}

let user = new User("Jack");

... basically the class definition from codecademy, without the specific constructor() syntax ...

So what is a constructor function??? Is it required to use the constructor() or not??
Was this page helpful?