What is the disadvantages

let animal = new Animal();

There are some disadvantages when using this syntax for inheritance, which are too complex for the scope of this challenge.
What is this disadvantage?

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Inherit Behaviors from a Supertype

Link to the challenge:

1 Like

The difference between two that Object.create(Animal.prototype) will not invoke constructor and thus will not create an instance. I absolutely have no idea how anyone might find it better in any any way, in fact it’s somewhat suicidal, because it clearly won’t be an instance, but following will be true, which is bollocks:

animal instanceof Animal; /* true .... SAY WHAAAT??? */

Spoiler alert: in the next challenge you will be shown somewhat shady example of how not running constructor but inheriting prototype might be useful - when you want to “append” random object into inheritance chain. I really really struggle to understand why would someone do that either, so if anyone has any clues - go ahead! :slight_smile:

You definitely should create instances using new operator, it is not even a matter of disadvantages rather it is the only correct way of doing it.

2 Likes

Isn’t new Animal() the same as Object.create(Animal.prototype), since the new operator is supposed to create a new object instance that inherits from Animal.prototype?

Object.create(Animal.prototype) would be the first step when you use new operator, but new keyword will also run constructor function on top. So by former option you will only do a half of work to create an instance - JS still considers these unfinished instances as instances, as it only cares whether they are in prototype chain or not. This approach will bite on every occasion:

const dateWithNew = new Date();
const dateWithCreate = Object.create(Date.prototype);

dateWithNew.getFullYear(); // 2020
dateWithCreate.getFullYear(); // TypeError
2 Likes

This is clear now.
Thank you for your reply