javascript constructor function vs factory function

Solutions on MaxInterview for javascript constructor function vs factory function by the best coders in the world

showing results for - "javascript constructor function vs factory function"
Isabel
06 Jun 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
Giulia
01 May 2016
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
constructor function vs classwhat is the difference between a constructor function and factory function what is differnce between constructor functions and factoryfunctionsjavascript constructor vs classjs factory functions vs classesfactory function vs class javascriptjavascript class vs factory functionconstructor vs factory javascriptfactory function vs constructor javascriptfactory vs constructor function javascriptjavascript constructor function vs classnew function vs new classfunction vs class jsjs factory functory functions for constructorfactory functions vs classes in jsjs factory function vs classfactory function vs constructorconstructor functions vs constructor in an objectfactory javascript es6 classesclasses vs factory functionjs new vs factory functionshould you use a factory function or an constructor function javascript function constructor vs var functionfactory function vs constructor function javascriptclass vs function factory typescriptfunction constructor 28 29 vs constructor 3d 28 29 3d 3e 7b 7dusing function constructor vs class javascriptchange factory function to constructor fnctionwhat is differnce between factory function and constructor function in javascriptfactory vs constructor vs class jsfactory function javascriptjavascript factory functiondifference between factory function and constructor function in javascriptes6 class vs function constructorjavascript class vs functionconstructor function vs factory functionsfactory functions vs constructor functionsfactory vs constructorjavascript classes vs factory functionsconstructor function vs class javascriptjavascript class vs constructor functionjavascript constructor function vs factory functionconstructor vs factory jsclass vs constructor function javascriptjs constructor vs functionconstructor vs factory fucntion in jsjavascript factory pattern vs constructorconstructor vs factory functionjavascript factory function vs constructor vs classwhat is differnce between constructor and factory function in jsfactory functions diff object constructorjavascript factory function vs constructorconstructor and factory functions javascriptconstructor function vs factory functionjs factory vs constructorfactory function constructorjs constructor vs factoryfactory method vs constructor typescriptfactory vs constructor javascriptconstructor vs function javascriptjs class or constructor functionfactory function vs constructor functionfactory function vs constructor in javascriptjavascript factory functions vs constructor functions vs classeswhen to use factory function vs classesfactory method vs constructorfactory function with constructorjavascript constractor function vs factory funcitonthe best between factory function and class javascript es6constructor vs method call javascriptconstructor vs factory p5 jsjavascript constructor function vs factory function