Difference between setting inheritance with Object.create() and prototype.__proto__

what is the difference between setting inheritance with Object.create() and just adding it to the prototype.proto.

function Animal() {}

Animal.prototype = {
  constructor: Animal,
  eat: function() {
    console.log("nom nom nom");
  }
};

function Dog() { }

// what is the difference
Dog.prototype.__proto__ = Animal.prototype
Dog.prototype = Object.create(Animal.prototype)

Challenge: Set the Child’s Prototype to an Instance of the Parent

Link to the challenge:

They are just alternatives to one another, they do the same thing.

The key difference is that use of the first (using __proto__) is extremely discouraged due mainly to the fact it’s deprecated. There is absolutely no guarantee that it will work, browsers are free to not implement it.


Note: it is important at this point in the curriculum to understand that you can change prototypes and how to do it.

However, IRL it’s an unusual thing to do: it’s a very slow operation (relative to anything else) and fairly manual and risky (you are deliberately breaking inheritance).

But if IRL this is a requirement, then Object.setPrototypeOf or Reflect.setPrototypeOf are what you’d use, like

Object.setPrototypeOf(Dog, Animal.prototype);
2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.