- [Instructor] In the last lesson we demonstrated
how we can improve our code
for setting up multiple types of shoppers
that essentially have the same shopping lists
by using the prototype pattern.
To do so we created a scout prototype.
And then we will clone the scout prototype
whenever it is we want to create new shoppers
that have the same list that a scout would have.
In this lesson we actually have to make this code work
by adding a clone method to our shopper class.
So within our exercise files under chapter two lesson five
I'm going to go into shopper.js file
and I'm going to go ahead and add our clone method
so that we can implement the prototype pattern.
So go ahead and say clone.
So the first thing I want to do
is I want to copy the copy the object structure.
And in java script the structure of this object
contains the methods add items to lists,
a getter for shopping lists, getters and setters
for the name value, as well as a constructor.
So what we're going to go ahead and do
is copy all of the prototype methods
that we see here on the screen,
the getters and setters for name,
the getter for the shopping list
and the add item to list, simply by grabbing
the current prototype for the current instance.
So I'm going to say,
var proto = object getPrototypeof (this).
And this will actually give me the prototype
of the current shopper object
which is our name, getters and setters for name,
shopping list and add item to list.
And now I'm going to create a new object
cloned = object.create proto.
So using the object.create we will create a new object
on the fly and add the current prototype
of the current instance to that object.
Now that gives us the methods of the current object
but we also want to clone any current values.
So we want to make sure the current shopping list
and the current name is also cloned
by creating copies of those values.
So our cloned object will have a ._name
of this._name.
So whatever this object is currently named
will be our cloned name.
Our clone shopping list is also going to equal an array.
So what we want to do is make a copy of the array.
And I'm going to do so by setting the shopping list
to a new array to square brackets.
Now using the spread operator I can copy all
of the values from this _shopping list
into our cloned shopping list.
And the last thing that we want to do is
we're going to return the cloned object.
So there we go.
Adding that clone method to the shopper class
will allow us to use the prototype pattern
when we create new shoppers off of the scout prototype.
Let's go ahead and give it a shot.
So I'll go over here to the terminal
and I will type node index.js
and we can see that we have two instances of shopper objects
that both were created using the scout prototype.
So they both have camping knife, tent, backpack and map.
All of that set up was obtained through the scout prototype.
So that's where all of the items were added
and now we can create as many shoppers
that are going to go on this camping trip as we like
using the scout prototype clone method.