Constructor Javascript

hello guys,
does anyone know what’s the difference between constructor(…) and function Dog(…) ?

function Dog (breed) {
  this.breed = breed;
}

class Dog {
  constructor (breed) {
    this.breed = breed;
  }
}

These are functionally the exact same thing.

The class syntax has a few minor improvements – eg if you try to run Dog("mutt") you will get an error saying you need to run new Dog("mutt").

It also puts everything in the same place, in one visual container.

And it makes inheritance a lot clearer –class Poodle extends Dog {. It also allows for subclassing builtins (class MyArr extends Array {) which couldn’t be done properly prior to class syntax.

1 Like

thank you so much!
now everything is clear

1 Like