JavaScript Factory Pattern
JavaScript Factory Pattern
This is an example on how to apply the factory pattern.
Implementation
base class and two child classes
// Base class User
// Subclass Administrator extending User
// Subclass Editor extending User
Factory function to create users
// Factory function to create users
Main program
// Using the factory function
// Output: "Admin has the role of Administrator"
// Output: "Editor has the role of Editor"
In this example we create users with the help of a factory function.