Factory Function

Motivating Factory Functions

  • There are three ways to create objects:

    • Constructor Functions
    • Classes
    • Factory Functions
  • In JavaScript, each function is a function object
  • An object is a collection of properties
  • A property is an association between a name (or key) and a value
  • Each of these approaches stores methods on a shared prototype
  • Implying, these approaches offer most of the same features

Creating Objects with Constructor Functions

// constructor
function ConstructorCar() {}

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

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

Creating Objects with Classes

// class
class ClassCar {
  drive () {
    console.log('Vroom!');
  }
}

const car2 = new ClassCar();
console.log(car1.drive());

Creating Objects with Factory Functions

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

const factoryCar = () => Object.create(proto);

const car3 = factoryCar();
console.log(car3.drive());

Comparing Classes to Constructors

  • Prototype functions have been around before ES6
  • Classes were included in the ES6 release
  • ES6 classes desugar constructor functions

Benefits of Factories and Constructors

  • Inheritance is easier with prototypes
  • Private properties better suited for factories

References

Previous
Next

Prototype

Function Closures