samedi 6 février 2016

Factory pattern vs constructor pattern in javascript

I saw a tutorial about pattern in javascript. However tutorial was good but it left me with few question.

As I see Factory and constructor produces the same result. So what is difference between both of them. What are the case scenario for each one.

Factory pattern

function factoryPattern(data) {
  var factory = {};
  factory.name = data.name;
  factory.title = data.title;
  factory.startDate = data.startDate;
  return factory;
}
var factoryUse = factoryPattern(jsonObj);

Constructor pattern

function constructorPattern(data) {
  this.name = data.name;
  this.title = data.title;
  this.startDate = data.startDate;
}

var constructorUse = new constructorPattern();

Aucun commentaire:

Enregistrer un commentaire