JSFiddle - React, Tailwind, and code Playground

by 533135

HTML

FACTORY PATTERN

JavaScript

function titan( options ) {

  // some defaults
  this.watch = options.name || "titan raga";
  this.state = options.state || "brand new";
  this.color = options.color || "silver";

}
function food( options){

  this.product = options.product || "water plus";
  this.category = options.category || "beverages";
  this.cost = options.cost || "20 rs";
}
function IT(options){

  this.client || "Home depot";
  this.category = options.category || "retail";
  this.from = options.from || "2007";
    alert(options.client)
}

function  tataGroup() {};// absract class

tataGroup.prototype.defaultClass = IT;
tataGroup.prototype.chooseDept = function(dept){
if(dept == "food")
    tataGroup.prototype.defaultClass = food;
    else if(dept == "titan")
    tataGroup.prototype.defaultClass = titan;
    
    return new this.defaultClass(dept)
};
var tatgrpObject = new tataGroup();
var newinstanceofIT = tatgrpObject.chooseDept(
{client:"sbi",category:"banking",from:"dont know !!!!"})

alert(newinstanceofIT instanceof IT)
//tatgrpObject.defaultClass({client:"citi",category:"banking",from:"2007"})