Jump to Categories

Factory pattern

var animal = function(animal, noise) {
  var actions = {};
  actions.animal = animal;
  actions.makeNoise = function() {
    return noise;
  }
  return actions;
}

var pig = animal('pig', 'Oinkkk');
// Pig
console.log(pig.animal);

var dog = animal('dog', 'Wooofff');
// Wooofff
console.log(dog.makeNoise());

Codepen example