Inheriting own properties of a constructor into an instance of another constructor

This question is not really related to this challenge.But i sincerely hope that my doubts are solved without confusing the person who answers it.

i want the object lion which is an instance of Dog to be able to access the property name in function Animal so i have written some code which i am not able to understand properly , as i have not fully understood OOP in JS.

1st doubt:
Dog.prototype = Object.create(Animal);
what does this line even mean ? and why does it not work?
I know we usually write
Dog.prototype = Object.create(Animal.prototype);
but since i wanted to access the own property name in function Animal, I thought about writing the code like this

2nd doubt:
The output for the code i wrote is displaying
Animal
why does
console.log(lion.name)
display Animal as output.

Your code so far


function Animal() { 
this.name="My name is Lion"
}

Animal.prototype = {
constructor: Animal,
};

function Dog() { }
Dog.prototype = Object.create(Animal);

let lion = new Dog();
console.log(lion.name)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15.

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

Link to the challenge:

1 Like

1st doubt:
Dog.prototype = Object.create(Animal);
what does this line even mean ? and why does it not work?
I know we usually write

okay so
var a = 2;
would set a with a value of 2 right?

now we have
const Dog= { //Dog is the parent

chiwauwa [“woof”], //child 1 elem

prototype [“woof woof”] //child 2 elem

};
So to call upon the woof woof we need to call up Dog —>prototype
which hold the value of woof woof

as we saw before with or variable
we now will set the whole thing
to Object.create(Animal)
which takes the name Dog and the prototype which includes woof woof to
the new Object.create(Animal)
Or like FCC says it: Remember that the prototype is like the “recipe” for creating an object. In a way, the recipe for Bird now includes all the key “ingredients” from Animal .

2nd doubt:
The output for the code i wrote is displaying
Animal
why does
console.log(lion.name)
display Animal as output.

Because it’s the parent
Just like Dog is the parent of chiwauwa
Dog is the collective name
chiwauwa is a dog species

I am sorry but i did not understand your method of explanation…maybe i am bit dumb…
could you explain both doubts again in another simpler way

i thought this line of code meant you are creating an empty object then assigning Animal as the prototype of Dog…then

would display “My name is lion” as output since lion which is an instance of Dog and has prototype Dog.prototype and it inherits all the properties of Animal which includes .name.

thank you for replying so quickly tho…and i hope you can explain it properly and in simpler terms

I’m just giving you some ideas.

Object.create(object) takes an object as input, not a function (like Animal)

We could use Dog.prototype = Animal.prototype but this overwrites the prototype of Dog, including the constructor.

So Dog.prototype = Object.create(Animal.prototype);

is the most common way to inherit from other constructors (apart from classes).


As for the second question, there are many ways, but that’s not quite the way we would use constructor functions I believe. Though I will try to come up with something.

Aligned to what I wrote before, this would be a simple way to do it:

function Animal() { 
}

Animal.prototype = {
constructor: Animal,
name:"My name is Lion"
};

function Dog() { }
Dog.prototype = Object.create(Animal.prototype);

let lion = new Dog();
console.log(lion.name)

If you don’t want all the objects to be named like that, here is another option:

function Animal(name) { 
this.name=name
}

Animal.prototype = {
constructor: Animal,
};
let pseudoLion = new Animal("My name is Lion")
function Dog() { }
Dog.prototype = Object.create(pseudoLion);

let lion = new Dog();
console.log(lion.name)

this method probably has pseudoEffects too :sweat_smile:

Aren’t functions objects ? According to

Also your psueudoLion answer is amazing .
i dint think about that but i don’t understand how is it working

why by creating new object pseudoLion and putting it in Object.create() solves the problem that i had

again…why cant we put Animal inside Object.create() since functions are objects.

and also my 2nd doubt is still unanswered…

I dint understand what you meant by this .
My original question was -
console.log(lion.name)
why does it give output as Animal…like why Animal …i believed output would be undefined or My name is lion…for me output as Animal was unexpected

Yep, I will expand a bit.

When you define a function, a Function constructor is called, and it sets the new function’s name, among other properties. It also inherites from Function.prototype.

function functionX(){} is an instance of Function: new Function(functionX). (kind of)

Because functions are created in this way, we see they are closer to objects than it seems at first sight.


A handy Example

function hey(){}
hey.name //logs hey.

This implies that function is an object with a property of name :slight_smile:
If you write lion.name javascript will look up name in this order:

  1. Is name in the object? Yes:prints name. No: goes to prototype.
  2. Is name in the prototype? Yes:prints name. No: goes to prototype.
  3. Finds name:Animal in the prototype, cause that’s the Animal.name, and you moved that to Dog.prototype.

PS:Sorry for the edits, I’ve tried to shorten this AMAP.

1 Like