0

Are the following two constructions identical, or are there any differences between them?

let x = new Item(v1, v2, ...)

And:

let x = Object.create(Item.prototype)
x.constructor(v1, v2, ...)

The code I was writing to see which one to use is the following (though for academic purposes):

'use strict';
function Meal(type) {
    this.type = type;
};
Meal.isMeal = function(obj) {console.log(obj instanceof Meal)};
Meal.prototype.eat = function() {console.log(`${this.type} has been eaten!`)};

// let breakfast = new Meal('Breakfast');
let breakfast = Object.create(Meal.prototype);
breakfast.constructor('Breakfast')

Meal.isMeal(breakfast);
breakfast.eat();
David542
  • 104,438
  • 178
  • 489
  • 842
  • They're obviously not *identical*. But in your `Meal` case, they can both serve the same purpose. – Bergi Feb 05 '22 at 05:18
  • 1
    "*to see which one to use*" - to create a new `Meal` instance, just use `new`. Why make it complicated? If you can't see a difference or advantage, go for the idiomatic solution. – Bergi Feb 05 '22 at 05:20
  • @Bergi I see thanks for those links. It seems like they serve the same purpose, so that answers my question, thanks: https://stackoverflow.com/a/17952160/651174 – David542 Feb 05 '22 at 05:34

0 Answers0