Difference between two JS Prototype inheritance

I want to know is there any difference between this topic which says to
use inheritance in object prototypes. actually there is two ways:

1- instead of adding same prop or method to object subtypes you add it to
a supertype object now they all have this methods.
2-use Object.create(obj) to make the subtype prototypes equal to the supertype.

  **Your code so far**

function Animal() { }

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

function Dog() { }

// Only change code below this line

let beagle = new Dog();
  **Your browser information:**

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

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

Link to the challenge:

Not an expert, but this is my understanding. Basically what they show is different ways that prototype property is set. The prototype object is inherited in instances of a constructor function.


function Adult(name,surname,hasChildren){
//all adult instaces have this props
//const Me = new Adult("Mister", "X", true);

function Child (name,surname){
//all Child instances have those props
Child.prototype = Object.create(Adult.prototype)
//we modify one
//const mySon = new Child("Mister", "Y")

Any instance of Child will inherit Adult’s prototype; some of these we update (i.e moreThan20).

But it is not the only way to use Object.create():

The Object.create() method creates a new object, using an existing object as the prototype of the newly created object.

So we could pass this instead:

const aRandomObject = {hello:'world'};
Child.prototype = Object.create(aRandomObject)

The other way to inherit, for ‘direct instances’ is just using the object constructor. For example any new Child() inherits from Child prototype.

1 Like

that’s what I wanted to know.so they basically do the same thing in different ways. thanks

1 Like

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