There are some disadvantages when using this syntax for inheritance, let animal = new Animal()

In the Inherit behaviours from Supertype challenge the title is stated:

There are some disadvantages when using this syntax for inheritance, let animal = new Animal()

I noticed similar topic was made but no concise answers were given, so what exactly are the disadvantages of using ‘new’ for creating instances?

Inheritance is defining a created object as an instance of a given class. This makes a dependency between them. We’ve defined a relationship between them, and of all programming relationships, three class-instance relationship is the most tightly coupled. Making it the most brittle.

In other languages, an instance of a class has those class methods. In javascript, what we call inheritance might better be understood as “method delegation.” The instance doesn’t inherit the constructor’s prototype methods or properties, it inherits access to them. In effect, by the prototype chain, objects have rights to shared code on the constructor classes. If our object doesn’t have a given method, javascript asks the constructor prototype if it does, on up the chain until the end of the chain (the Object constructor, from which all objects descend).

Now, so far, no real drawbacks. In fact, this is all great - by having potentially hundreds of instances all share a single constructor reference, we really save memory! We don’t have hundreds or thousands of copies of those prototype methods, we have one. Nice!

Until, somewhere down the line, someone changes a method on that constructor prototype. You can, at any point, it’s nothing special. Just another object.

But when that happens, if our instances are not aware that their constructor has been changed…things break.


Ouch… I think Ill reread that after a good night’s sleep!

So how does let animal = Object.create(Animal.prototype) evade that problem (“alternative approach without those disadvantages”)?

I can’t speak for the course, i didn’t write it, but to me its about intent.

Here’s what happens when we do let animal = new Animal():

  1. we create an empty object, {}, somewhere in memory.
  2. we run the Animal() function, using that new empty object as the this reference (the context) of the function. Doing that, any this reference refers to the new object, thus populating that with whatever properties we will be creating on the new thing.
  3. We reference the new object’s prototype property to the Animal.prototype, so any methods that don’t exist on our new object can be searched on the Animal.prototype shared object.
  4. that newly populated object, connected to its prototype chain, is now “wired” to our variable animal, completing the declaration and assignment.

When we do let animal = Object.create(Animal.prototype);, we are explicitly and intentionally doing something similar:

  1. create a new empty object, {}, somewhere in memory.
  2. set the prototype of that empty object to the Animal prototype, wiring the empty object into its prototype chain.
  3. assign the object reference back to our variable.

We don’t have the explicit creation of content step. But we are intentionally wiring the new object instance to the thing that has the methods we want to share.

By doing let animal = new Animal(), we are telling ourselves “we are creating a new instance of this class.” Easily done, and not wrong, but if we are coming from a language that is truly “class-based”, we are telling ourselves something that might not be entirely true. What we are doing is not creating an instance of a class, what we are doing is creating a new object and defining its prototype chain.

The first is misleading, the second is… useful. :wink:


So it appears that those disadvantages are most likely negligible at worst and one shouldn’t worry about using the ‘new’ keyword, I won’t mark your answer as solution only in case someone disagrees and wants to provide further more significant points regarding on what the creators of the course were thinking about when saying there are disadvantages with using the “new” keyword.

Don’t know the mechanism of this forum, if it has to be closed automatically, or by mods I wouldn’t mind, thanks for your insight.

5 months later but for anyone interested I ran into this article which outlines the problems that may arise when using the ‘new’ keyword in JavaScript

I find these all the time. There are folks who will tell you classes or inheritance in Javascript are bad - and usually, this is because of a poor understanding of classes, constructors or inheritance as another tool in our toolbox.

Or we’ll hear folks saying how terrible forcing functional patterns in Javascript is, when we have clean classes and inheritance. And usually, this is because the commenter has an incomplete understanding of functional programming, in Javascript, as another tool in that same toolbox.

Constructors are incredibly powerful, inheritance incredibly useful. Functional programming in Javascript is also powerful and flexible and useful.

But both are simply tools we have available. If we choose to say “metric wrenches are BAD”, we can expect to not work on things requiring them… Which cuts a huge part of are toolbox out entirely.


Also note that this article is from 9 years ago. Javascript is very different nowadays.

1 Like