1

What is the difference between a constructor function that creates an object like this:

function User(name) {
  this.name = name;
  this.isAdmin = false;
}

let user = new User("Jack");

and a non constructor function that looks like this:

function user(name, age) {
  return {
    name,
    age,
  }
};

let user = user("Tom", 23);

I am currently learning about constructor functions and it does not make sense for me to use them if you can replace them with the function above. Can anybody explain how a constructor function is more useful in practise?

Damian Kowalski
  • 362
  • 2
  • 8
  • One useful feature is easy “type” checks: `user instanceof User`. There’s also inheritance, but this could be implemented like `function cat(){ return {...animal(), noise: "meow"}; }`. – Sebastian Simon Dec 26 '18 at 03:20
  • 1
    If it helps, I've posted an [answer](https://stackoverflow.com/a/53927681/5217142) to the older question using your examples. – traktor Dec 26 '18 at 05:18

0 Answers0