Factories create an object and return it. That's all. The created object costs one, and the best thing is that you can use this object, and not be what happens to other objects. This is known as singleton.
var Car = function(){ var car = {}; car.running = false; car.toggleEngine = function(){ this.running = !this.running; } return car; }; car1 = Car();
Constructors add code to the function, so you have a link to the prototype of the object constructor. A good part of this sitelink is to use a functional common technique that looks like this.
var Car = function (){ this.running = false; }; Car.prototype.toggleEngine = function(){ this.running = !this.running; } var car1 = new Car;
As we see after creating the objects, they were still very connected.
To be clear, you can still do the following and not affect the objects created by the constructor. Using the functional general method and mask the prototype function specified by the constructor. Thus, they are not fully connected, but connected through the prototype constructors.
var car1 = new Car; //running false var car2 = new Car; //running false car2.toggleEngine() //running true car2.toggleEngine = function(){}; car1.toggleEngine() //running true
source share