Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript constructor function vs factory function

function ConstructorFunction() {
   this.someProp1 = "1";
   this.someProp2 = "2";
}
ConstructorFunction.prototype.someMethod = function() { /* whatever */ };

function factoryFunction() {
   var obj = {
      someProp1 : "1",
      someProp2 : "2",
      someMethod: function() { /* whatever */ }
   };
   // other code to manipulate obj in some way here
   return obj;
}
Comment

factory function vs constructor javascript

// constructor
function ConstructorCar () {}
ConstructorCar.prototype.drive = function () {
  console.log('Vroom!');
};

const car2 = new ConstructorCar();
console.log(car2.drive());

// factory
const proto = {
  drive () {
    console.log('Vroom!');
  }
};

const factoryCar = () => Object.create(proto);
const car3 = factoryCar();
console.log(car3.drive());

// class
class ClassCar {
  drive () {
    console.log('Vroom!');
  }
}
const car1 = new ClassCar();
console.log(car1.drive());
Comment

PREVIOUS NEXT
Code Example
Javascript :: google analytics nextjs 
Javascript :: js number in range 
Javascript :: vuex store in js file 
Javascript :: function shorthand javascript 
Javascript :: json_extract in non native query 
Javascript :: datepicker range npm reactjs 
Javascript :: javascript access map elements 
Javascript :: Modify String with Uppercase 
Javascript :: javascript loading animation on button click 
Javascript :: find in js 
Javascript :: how to get last element of an array 
Javascript :: binarysearch 
Javascript :: send data from form to another page angular 
Javascript :: How to make a toggle button in Angularjs 
Javascript :: react.dom 
Javascript :: push an item to array javascript 
Javascript :: how to turn of autocomplete in react hook form material ui 
Javascript :: page scrolling react js 
Javascript :: Promise.all() with async and await to run in console 
Javascript :: getattribute 
Javascript :: JavaScript Error Try Throw Catch 
Javascript :: map and set in javascript 
Javascript :: import in react 
Javascript :: search in array javascript 
Javascript :: javascript symbol 
Javascript :: how to write last element of array 
Javascript :: find in javascript 
Javascript :: javascript meme 
Javascript :: react lifecycle hooks 
Javascript :: google app script 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =