Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Constructor pattern and Prototype pattern

So I'm trying to wrap my head around the different ways to create an object.

I came accross the Protoype pattern for creating objects.

Now I wrote two functions below but I can't see what the functional difference between both would be? When would you use the Constructor pattern and when would you use the Prototype pattern?

Constructor Pattern

function Fruit(){}
Fruit.color = "Yellow",
Fruit.fruitName = "Banana",
Fruit.nativeTo = "SomeValue"

Prototype Pattern

function Fruit(){}
Fruit.prototype.color = "Yellow",
Fruit.prototype.fruitName = "Banana",
Fruit.prototype.nativeTo = "SomeValue"
like image 274
Marco Geertsma Avatar asked Jan 28 '16 09:01

Marco Geertsma


People also ask

What is the difference between constructor and prototype?

So what's the difference between constructor and prototype? A short answer is that the constructor is a function that is used to create an object, while the prototype is an object that contains properties and methods that are inherited by objects created from a constructor.

What is constructor pattern?

In classical object-oriented programming languages, a constructor is a special method used to initialize a newly created object once memory has been allocated for it. In JavaScript, as almost everything is an object, we're most often interested in object constructors.

What is the difference between __ proto __ and prototype?

The prototype property is set to function when it is declared. All the functions have a prototype property. proto property that is set to an object when it is created using a new keyword. All objects behavior newly created have proto properties.

What is the difference between factory and prototype design pattern?

Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through subclassing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned.


1 Answers

Reusability of the components...

Constructor

When you create a new constructor you will create a new instance of everything and importantly any change made to the instances will only affect them and not the others.

Prototype

When you create a new object using the prototype it will reuse the logic and any change to the prototype chain will affect everyone else.

This is a nice explanation: Javascript prototypes and instance creation

When to use each pattern is based on needs - very ambiguous answer but never-the-less the situation.

Think of the Object, Function, Array they are used throughout JS and would make sense that they live on the prototype chain as any changes we have to them we would want to propagate - a side note: which is why we should never alter these as it can screw up their behavior.

Best explanation here: JavaScript constructors, prototypes, and the new keyword

like image 178
Sten Muchow Avatar answered Sep 27 '22 20:09

Sten Muchow