The real reason for constructor function

Hi, I’m learning JS by following MDN’s Learn web deelopment. In Classes in JavaScript - Learn web development | MDN, it introduces construction function using the code

function createNewPerson(name) {
  const obj = {};
  obj.name = name;
  obj.greeting = function() {
    alert('Hi! I\'m ' + obj.name + '.');
  };
  return obj;
}

and the conclusion

This works well enough, but it is a bit long-winded; if we know we want to create an object, why do we need to explicitly create a new empty object and return it?

but this is not enough to introduce construction function to me, since what about returning the whole object at a time? it works as well according to my test, it is not long

  function createNewPerson2(name) {
      return {
          name: name,
          greeting: function () {
              alert(`Hi! I'm ${this.name}.`);
          }
      };
  }

  const p1 = createNewPerson2("akk");
  console.log(p1.name);
  p1.greeting();

Hi @Xu_Chunyang!

You are correct that you can write this in a shorter form, but this is still basically doing the same thing. And it doesn’t have any of the other other advantages of OOP, such as inheritance.

Saying that, it makes me wonder whether I should rewrite the conclusion bit, as it doesn’t really accurately get across why using a constructor is better. That doesn’t really become apparent until later in the course.