showing results for - "factory function vs constructor javascript"
Lilou
08 Mar 2020
1function ConstructorFunction() {
2   this.someProp1 = "1";
3   this.someProp2 = "2";
4}
5ConstructorFunction.prototype.someMethod = function() { /* whatever */ };
6
7function factoryFunction() {
8   var obj = {
9      someProp1 : "1",
10      someProp2 : "2",
11      someMethod: function() { /* whatever */ }
12   };
13   // other code to manipulate obj in some way here
14   return obj;
15}
16
Nils
17 Nov 2017
1// constructor
2function ConstructorCar () {}
3ConstructorCar.prototype.drive = function () {
4  console.log('Vroom!');
5};
6
7const car2 = new ConstructorCar();
8console.log(car2.drive());
9
10// factory
11const proto = {
12  drive () {
13    console.log('Vroom!');
14  }
15};
16
17const factoryCar = () => Object.create(proto);
18const car3 = factoryCar();
19console.log(car3.drive());
20
21// class
22class ClassCar {
23  drive () {
24    console.log('Vroom!');
25  }
26}
27const car1 = new ClassCar();
28console.log(car1.drive());
queries leading to this page
factory function with constructorjs constructor vs functionfactory function vs constructor function javascriptusing function constructor vs class javascriptconstructor vs factory p5 jsjavascript constructor vs classfactory vs constructorfactory function javascriptfactory functions vs constructor functionsfactory method vs constructorjavascript factory pattern vs constructorjs factory functory functions for constructorwhat is the difference between a constructor function and factory function js factory vs constructorconstructor vs function javascriptjavascript function constructor vs var functionjavascript class vs functionconstructor vs method call javascriptjavascript classes vs factory functionsfactory vs constructor function javascriptchange factory function to constructor fnctionjavascript class vs factory functionclass vs constructor function javascriptclasses vs factory functionfactory javascript es6 classesclass vs function factory typescriptjs factory function vs classconstructor and factory functions javascriptfactory vs constructor vs class jsconstructor vs factory jsfactory functions vs classes in jsjavascript factory function vs constructorfactory method vs constructor typescriptjs constructor vs factoryjavascript factory function vs constructor vs classjavascript factory functions vs constructor functions vs classesjs class or constructor functiondifference between factory function and constructor function in javascriptconstructor functions vs constructor in an objectjavascript factory functionfactory vs constructor javascriptconstructor vs factory javascriptfactory function vs constructorfactory function vs constructor functionconstructor function vs factory functionconstructor vs factory functionconstructor function vs classfactory functions diff object constructorwhat is differnce between factory function and constructor function in javascriptfactory function vs constructor javascriptwhat is differnce between constructor and factory function in jsjavascript constructor function vs factory functionwhen to use factory function vs classesconstructor vs factory fucntion in jsthe best between factory function and class javascript es6javascript constructor function vs classnew function vs new classfunction constructor 28 29 vs constructor 3d 28 29 3d 3e 7b 7dwhat is differnce between constructor functions and factoryfunctionses6 class vs function constructorfactory function vs class javascriptjavascript class vs constructor functionfunction vs class jsfactory function vs constructor in javascriptfactory function constructorconstructor function vs class javascriptshould you use a factory function or an constructor function js new vs factory functionjs factory functions vs classesconstructor function vs factory functionsjavascript constractor function vs factory funcitonfactory function vs constructor javascript