Prototype Pattern
Prototype pattern is one which creates objects based on a template of an existing object through cloning.
We can think of the prototype pattern as being based on prototypal inheritance where we create objects which act as prototypes for other objects. The prototype object itself is effectively used as a blueprint for each object the constructor creates.
One of the benefits of using the prototype pattern is that we're working with the prototypal strengths JavaScript has to offer natively rather than attempting to imitate features of other languages.
Not only is the pattern an easy way to implement inheritance, but it can also come with a performance boost as well, when defining a function in an object, they're all created by reference (so all child objects point to the same function) instead of creating their own individual copies.
We can use Object.create (introduced by ECMAScript 5, To remind ourselves, Object.create creates an object which has a specified prototype and optionally contains specified properties as well) for implement prototype pattern.
var vehicle = { model : "N/A", accelerate : function(){ console.log("accelerate"); }, wheelsNo : 4, getDriverName: function(){ console.log("Driver: " + (this.driverName || "N/A")); } }; var myCar = Object.create(vehicle); var myFriendCar = Object.create(vehicle, { model : { value : "Ford" }, doorsNo : { value : 2 }, driverName : { value : "Michael Philip" } });The above code we create our blueprint (vehicle) and use Object.create for create new object using it as a template, we can also suppose that we create a basic inheritance (vehicle) as a parent and myCar, myFriendCar are children.
We Create myCar object, and it clone every thing from parent, if we called getDriverName, it will log "N/A" because we not support it witj driverName and of course there is no model.
But myFriendCar object we create it also using Object.create, and we support it with some properties like doorsNo (not exist on parent object but a property for this object only) , and we support model property with value, also we set driverName Property with value, so if we call getDriverName method it will return "Michael Philip".
So, rather than creating non-initialized objects it returns objects that are initialized with values it copied from a prototype or sample object. The Prototype pattern is also referred to as the Properties pattern.
Another example for classical implementation for prototype pattern a ClientBluePrint object that clones objects given a prototype object. Its constructor function accepts a prototype of type Client. Calling the clone method will generate a new Client object with its property values initialized with the prototype values.
var ClientPrototype = function (protoObj) { this.clone = function () { var client = new Client(); client.first = protoObj.first; client.last = protoObj.last; client.status = protoObj.status; return client; }; } var Client = function(first, last) { this.first = first; this.last = last; this.say = function () { alert("Full Name: " + this.first + " " + this.last); }; } var run = function() { var protoObj = new Client("Hamed", "Farag"); var prototypeObj = new ClientPrototype(protoObj); var client = prototypeObj.clone(); client.say(); } run();