Factories, Constructors, and Classes
William Turner, Steve Leonard · 2022
Factories, constructors are classes are almost identical. The difference lies solely in minor implementation details and syntax. The first pattern, called factory, is used to create your audio loader library. The second pattern, called constructor, is introduced primarily because of its prevalence in the JavaScript world, making it an important pattern to familiarize yourself with. The third pattern is called class and is a syntax that is used in modern JavaScript as an alternative to the constructor pattern. Programming languages that are organized around objects that interact with one another are usually referred to as object oriented. JavaScript is considered an object-oriented language, although it differs from traditional object-oriented languages in one important way: JavaScript originally lacked what are called classes. Objects created with factories, like all objects, can be extended to include additional properties and methods. Programming with factories is a common pattern in JavaScript and one that you should be sure to familiarize yourself.