You already know how to create objects using a literal or new operator.
And now I want to introduce another way to create objects using objects that create method.
This method allows us to create object and at the same time, it allows us to define for the object.
The prototype that we want objects to create is a method of a global object.
And in order to prove that, let's go to Brackett's.
I'm going to use a new method called Castledare, which allows us to display list of properties and
methods of the specified object.
So.
Right, Castle Dot, dear.
And inside the parentheses, that's right, the global object.
Then reload and we have here the object function, if we expand it, you will see here a bunch of methods
and some of the properties and here at the beginning we have our mentioned method create.
All right.
Let's get rid of council doctor and create the variable, the object one.
As we have just seen, create is a method of global object.
Therefore, we need to right at first object and then attach to it create method.
Object and create takes two parameters as a first parameter, we need to define the prototype that we
want to connect to our object, and you need to remember that passing this parameter is mandatory.
So.
Right.
Object that prototype.
The second parameter is optional, and I'm going to omit it for now and now, let's see and cancel what
we have, right.
Cancel that log, object one.
Reload, so we have here an empty object because we have missed second parameter, the exact same happens
if we use empty color braces here as a second parameter.
OK, if we drop down this empty object, we will get your global object functions prototype, it's because
we have indicated it as a first parameter inside object.
What great method.
We can say that it's the same thing as an empty object created using literal notation.
So right here for our object to and then log it in Castle as well.
Right.
Consider log object to.
Revote.
And you see that both of them have access on global object functions prototype.
All right, let's remove these lines of code and now I'm going to show you how to add properties and
methods to the newly created object.
Of course, we can use dot notation.
So if we write object, one dot name equals to John.
Then reload will have your name property inside our object.
There is another way to add properties and let's comment this line out.
We can do it using second parameter.
This way has kind of strange syntax, but it's the way how it works.
So write your name.
And here is a strange part.
Instead of assigning the value simply, we need to create another object and then use a value property
and then assign the actual value to it.
So open Calabrese.
All right.
The value.
John.
Reload the page, and he said that we have the exact same result.
All right, so you already know basically how object and create method works.
As we said, we can connect newly created object to any of the objects, prototypes and let's see it
with a function constructor.
Let's remove these codes and create a function constructor.
Right function person.
That inside that club, racist, right, this that name.
And as undervalued as John.
No, I'm going to add new property to persons prototype object, so right here, person dot prototype.
That's great equality, hello.
OK, as first I'm going to create object using new operator in order to compare it to the object that
will be created using object dot create method so right var person one equals to new person.
Then use objects that create and create other objects, the person to equals objects create.
And passed as a parameter.
Person, not Prototyp.
Let's log both objects in council, right?
That log person one.
And then person to.
Reload and you see that we have two person objects, first one which is created using new operator and
the second person object, which is created using objects, dot create method.
Let's expand them and drop down.
Underscore, underscore, Proteau, underscore, underscore properties as well.
You see that in both cases we have great property.
So it means that both objects have access and persons prototyp object.
And of course both of them have access on global object functions prototype.
But here we have one difference between those two person objects.
The first one which was created using a new operator has named Property and the Value John, but the
second one created using object, that great method doesn't have it.
OK, so you already know that using object to create method, we can manually define the prototype for
the created object, either the prototype of global object or function constructor, or even we can
create other objects and pass it as a prototype of it.
So it may sound confusing, but not sorry.
I'm going to show you another example in order to explain it well that I'm going to create an object.
Right?
VAR first Crothall.
Then right here, the method, say hello.
Then anonymous function.
And inside the the braces, right to return.
Hello.
Because of that, I'm going to use this entire object as a prototype.
I want to use variable this and name, which will be the property of current object.
So right here, plus this dot name.
And now I'm going to create object and set for Proteau object as its prototype.
Right.
VAR John equals two object dot create.
Pass here the first parameter, as we said, for Proteau object and then as the second parameter open
here, Colonel Brice's right to name property.
Then again, open coal prices, right value
as John.
That's Luggage and Council Rights Council, good luck, John.
Without.
Here we have objects with property name John, and if we check it's underscore, underscore, Proteau,
underscore, underscore property will find there say hello method from First Proteau object because
as you see, it is passed as a prototype for John object.
So again, object dot create method gives us the ability to use manually created objects as prototypes.
All right, in order to make our example more interesting, I'm going to create another object using
object and create method.
This object will be used as a prototype for another object, which will be created soon.
And we will see kind and interesting prototype hierarchy.
So right now, our second prototype.
Equals to object dot create.
And I'm going to pass as prototype the object that we have created, I mean, first Proteau.
Then open the coal prices and at hear the method, say hi.
Then we need to hear the value.
As anonymous function and inside the call braces right return.
Hi, plus this dark Nate.
Let's say it in Castle, White Castle, Deathlok, Second Proteau.
Reload, so here we have object with say hi method, and if we expand, underscore, underscore, Proteau,
underscore, underscore property, we will find say hello method from First Proteau object.
OK, and now the last thing that I'm going to do is to create another object and to assign to it as
Prototyp Second Proteau object so right var blob equals to object.
Thoughts create.
Then as the first parameter, as we said, let's pass second Proteau.
Then as the second parameter open cut prices and add in property and then the value as Bob.
Again, let's see Castle, right, Castle?
Good luck, Bob.
Reload.
So here we have object.
Bob, let's drop down it and check the first underscore, underscore, Proteau, underscore, underscore
property.
Here we have Sagi method because the prototype of Bob Object is set as second Proteau object.
If we expand another underscore, underscore, Proteau underscore, underscore property, then we will
find say hello method from first Proteau object because it is set as a prototype for second Proteau
object.
And then finally we will reach to global object functions prototype.
Makes sense.
All right.
I think it's not a very complicated topic, but again, I recommend you to think of other examples and
test this method on your own, because it's the best way to learn more.
So before we finish, I want to show one more thing.
Let's create another object.
So right below our object equals object, dot, create and as a parameter, let's pass here the null
value and then check in council what we have.
Right, cancel that log object.
Reload, then drop down it, you see that our object is completely empty, it doesn't have underscore,
underscore, Proteau underscore on the school property.
It means that this object doesn't have connection with any of the prototype objects, and that's because
of the null value.
OK, so if we compare object of creative function, constructor method, both of them have some advantages
and disadvantages which we actually have talked about already.
Nowadays, function constructor is more popular and flexible, but regardless of that, you may come
across the situations where you may need to use object that great method.
So you have to know well how it works.
All right, let's move on.