Assigning Parent object's prototype vs Object.create(Parent.prototype)

Hi all,

I’ve been following the Learn Web Development track and this is my first time on the forums.

Understanding Object prototypes and inheritance has been a super confusing journey so far. In this article we’re supposed to set Teacher()'s prototype and set constructor reference. A few questions:

  1. Why is the below code necessary after we’ve defined a Teacher constructor function using Person.call in the function body?
Teacher.prototype = Object.create(Person.prototype);
  1. Why can’t we do this instead?
Teacher.prototype = Person.prototype;

Apologies if this is a noob question, I just haven’t been able to wrap my head around this.

Hello @lakshverma23

do not be sorry and i also see that the coding style is confusing in js

so let me ask @chrisdavidmills to help you with that

and have a nice day both of you :slight_smile:

1 Like

Thanks @justsomeone! Would love to hear from @chrisdavidmills :slight_smile:

you very welcome :slight_smile:

Pushing this back up. Could anyone please help me understand this?

Hey @Ran @ayanda could you help us here

Will have a look tonight @Justsomeone.

thanks a lot @Ran :slight_smile:

Hi,

Apologies, just a bit busy these days. I think @lakshverma23 question is a great one. I am still looking into it. The way I work is to run the code itself and see the outcome.

So when we use

Teacher.prototype = Object.create(Person.prototype);

The outcome when I run

Teacher.prototype.constructor

on the console is
TeacherObjectCreate

when I run the it with the following

Teacher.prototype = Person.prototype;

the outcome when I run

Teacher.prototype.constructor

is the following
TeacherNoObjectCreate

You might notice the double forware slashes on the last image where it starts with “// Teacher.prototype = …”

so I am just wondering if there is any object inheritance in this case and if the prototype of teacher is person in this case. I m going to have to look a bit further and get back.

nice idea @Ran

think we need to call some of the methods defined in person using teacher object for both cases and see if both inhirit it or not

well done @Ran

Hi @justsomeone,

Apologies, my work has called me away. I might will try to come back in a month when I get a bit more settled with the new work.

Ran

Hello @Ran

it’s ok do not be sorry and congratulations for your new work and good luck

see you later and have a nice day :slight_smile: