Factory Functions in JavaScript
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about classes versus constructor functions in JavaScript, as a clear introduction to factory functions as an alternative.
Recommended by 3 people across 3 different months, 2016 to 2019.
What it is
A short episode of Fun Fun Function in which Mattias Petter Johansson (mpj) explains factory functions in JavaScript, using a simple dog object as the example. He presents them as a way to create objects with private state and without relying on classes or the `this` keyword.
What people said
Recommends Paul Page · 2017-10 · Stack Overflow · on “Class vs function returning object”
Neither one should have a very big impact on performance. While classes ( Counter1 in your example) technically have slightly better performance, this is only noticeable if you are creating several thousand instances of the class per second (which is probably a bad design choice anyway). As for other advantages, there are advantages and disadvantages to both. FunFunFunction is a great YouTube channel for learning about JavaScript, and it's where I first learned about factory functions ( Counter ). The channel owner strongly favors these over using class es. One of the reasons for this is that you can't get consistent behavior out of the this keyword with the way classes are implemented in JS
Recommends joshweir · 2018-11 · Stack Overflow · on “Adding Properties to existing Javascript Objects from Object Constructors”
You can add additional properties after the fact: function funcConstruct(first,last,age) { this.firstName = first, this.lastName = last, this.age = age, this.fullNameAge = function() { return this.lastName + ', ' + this.firstName + ', ' + this.age } }; var johnDoe = new funcConstruct('John', 'Doe', 52); johnDoe.foo = "bar"; You are using the function constructor pattern, but note that there are other javascript object encapsulation patterns you can use depending on your use case some are better than others. In my opinion, I would use the function constructor pattern only if I was using pre ES6, if using >=ES6 (ES2015) I would use a class [LINK] , and only if I needed a bunch of instances of
Recommends ellis hogan · 2019-10 · Stack Overflow · on “How does the 'class' syntax work in javascript?”
I would recommend staying away from classes if at all possible (In Javascript) checkout this video on factory functions with fun fun function [this video] for why factory functions are usually better. Class.prototype can be tricky, and the "this" keyword in javascript can be tricky without properly binding method calls with .bind(this). React even has you bind the method in the class. This is not to say classes are incorrect in any way, just that they can be unnecessarily complicated in javascript.
Cites the digitalmouse · 2016-06 · Stack Overflow · on “Efficient way of creating numerous factory objects in Javascript”
I'm tasked with a graphical menu system that is built on a wheel navigation model where each 'layer' of menu is composed of a new Wheel object with properties that define colors and menu choices and so on. I'd like to use a Factory method to build a single generic object creator for building all the layers, something akin to the dog factory from Mattias Petter Johansson at funfunfunctions: video: [this video] transcript: [LINK] Dog object as Factory method: const dog = () => { const sound = 'woof' return { talk: () => console.log(sound) } } // create a single new dog const sniffles = dog() sniffles.talk() // Outputs: "woof" This works well, and I get how it works. What stumps me a bit is how to '