0

EDIT: I understand the purpose of Object.create() in general. But i need to know why it is used in the below code as it makes no difference at all.

I found the below code in this article. I can understand that fruits.call(this); will essentially inherit the properties from fruits to any instance of apple that gets created. Then why do we need to do apple.prototype = Object.create(fruits.prototype); Can anyone explain the purpose of this line? I tried executing the code without the that line and it still runs with no issues.

function fruits() {
    this.name = 'fruit 1';
}

function apple() {
    fruits.call(this);
}

apple.prototype = Object.create(fruits.prototype);
const app = new apple();
console.log(app.name);

// Output: fruit 1
Haeram
  • 3
  • 1
  • 3
  • It will be useful if you ever add anything to `fruits.prototype` and then want to access those things in instances. – CertainPerformance Aug 29 '22 at 00:44
  • this way of working with objects and creating instances of them, is a bit deprecated. you can save yourself a lot of head scratching by looking into the `class` keyword. – Kokodoko Aug 29 '22 at 14:26

0 Answers0