The prototype clone method

Web Development September 30, 2018

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).

Gearloose

Welcome to my blog. My name is Akis Mparekas. I am a freelance web developer from Greece. At the same time I am taking my first steps in game development (unity) and animation (blender). I also create cool stuff, either for work or for fun. When I have time, I write tutorials and I blog.