[JS] Factory Functions
Instead of using the new keyword to create an object, factory functions set up and return the new object when you call the function. They do not use the prototype, which incurs a performance penalty - but as a general rule, this penalty isn’t significant unless you’re creating thousands of objects. Let’s take a basic example to compare them to constructor functions.
I don't understand what is meant by "they do not use the prototype"2 Replies
What I understand is that unlike constructor functions, factory functionsare gonna define methods on each object
they wont have the methods from the parent (prototype)
But one more quetion
they are still gonna have Object.prototype, right? coz its the prototype that everyone has
The subtle distinction:
Constructor functions:
instance → Constructor.prototype → Object.prototype
Factory functions (default):
instance → Object.prototype
So factory-created objects still have Object.prototype, but they don’t have that extra layer (Constructor.prototype) where you can hang shared methods.