Instructor: In this lecture we will talk
about another way that we can create objects
that inherit from a prototype,
and that's the object.create method.
We have seen the function constructor pattern
to create objects from a blueprint in the last two lectures,
but there are more ways to create objects and inheritance.
Let's talk about the object.create method now.
Let's first comment out this code here.
Okay.
So this is object.create.
All right.
With the object.create method things work a bit different.
In this case, we first define an object
that will act as the prototype and then create a new object
based on that very prototype.
Let's do that right now.
We're gonna recreate the person example
that we used for the function constructor lecture.
First, we have to write the prototype as a simple object.
I'm gonna call it personProto, so personProto,
and now you see we no longer use the capital P
in the beginning because it's not a function constructor.
What I want in a prototype, such as before,
is to calculate age method, so we already know the why
and how inheritance work.
Now we're just doing it in a different way.
Instead of using function constructors,
we will do it the object.create way,
so it's simply a different way.
All right so calculateAge and function,
and again I'm gonna console.log it
(2016 - this.yearOfBirth).
Now there is no year of birth here anywhere,
but we're going to take care of that later.
Okay, and now let's create John again.
So we simply say Object.create,
and then we simply pass it the object
that we define to be the prototype object
which will be personProto.
Okay, so let's see this in the console.
All right, and I'm not gonna console.log this,
so I will just do it the way we did before
in the last lecture so John, and here is the object.
Now it's empty, there's nothing here, right?
But we still have to prototype,
and this already has our calculate age function,
so this looks a little bit like before, doesn't it?
Now we just have to fill this John object here
with our data.
So we can simply do it the old way, right?
So john.name is John.
John.yearOfBirth is 1990.
And john.job is teacher.
And if I reload this now and say John
then here we have our object,
and it looks a lot like it did before
with the function constructor, right?
So we have our data here the job, name and year,
and then the prototype which contains
our calculate age function.
However, it was not ideal the way
that we had to fill this here, right?
The object that we created was empty
and then we had to fill it here with the data,
so this time we're going to do it a different way
because the object.create accepts a second parameter,
so let's now do that for another person.
Jane = Object.create.
And of course the prototype is, again, personProto,
but then we can pass a second argument.
Which is an object specifying the data
that we want to have in the object so name,
and now we cannot simply put the name here,
but we have to do it in this rather strange way like this.
We have to say value and then we can say Jane, okay?
This looks a bit strange, but it's actually how it works,
so there's no way around doing it like this, so yearOfBirth
and then again value and 1969
and then the job.
And once again value and then designer.
Okay, and let's now reload this.
John again and Jane,
so both share the same prototype, as you can see here,
because both have the calculate age method,
and then they have both their own type of data in there.
Okay, so let's think about this again.
The difference between object.create
and the function constructor pattern is
that object.create builds an object that inherits directly
from the one that we passed into the first argument.
While, on the other hand, the function constructor
the newly created object inherits
from the constructor's prototype property, right,
so that's the big difference,
and I hope this makes sense to you.
Actually, one of the biggest benefits of object.create is
that it allows us to implement
a really complex inheritant structures in an easier way
than function constructors because it allows us
to directly specify which object should be a prototype.
All right, so function constructors and object.create will
be the most common ways of creating objects
that you will find the most when you're looking
for tutorials on the web.
That's why I showed you both in this course.
The most popular, though, is still the function constructor,
and so that's the one that I'm going to use mostly
in the rest of this course.