Instructor: Let's say that we're working on an application that acted similar to a video game where we had characters. We might use objects to define their attributes.
Here we have an object called Tyler with attributes of hair and height. Let's create an object for Sally. For hair and height, we give her blond hair and she's 5'4".
Both of these objects are human characters. We should probably add a type property to both that say "human" as a string. Moving forward, all future human type characters we could manually add this type property every time we define a new human.
That's not very dynamic and could cause problems in future development if all of our humans were to turn into zombies. Another way would be to assign the prototype object of each human character object to a Human object.
This way, we're only writing out type human once. We'll use Object.setPrototypeOf to assign this human object to our human characters. Now we console.log(Tyler.type). We see we have access to Human.
This way does make it easier if our characters were to turn into zombies, but it seems messy and fragile to constantly assign the prototype object of each new human character object with this setPrototypeOf method. Instead, let's just remove this prototype delegation and change our human object into a createUser function that accepts a character and returns a new object where we spread out the character's traits and assign it type of human.
Instead of these objects, we'll actually call this function, passing through these objects of attributes. If we look at our console.log, we're back to type Human. By refactoring this into a function, we've created what's called a "factory function." Factory functions are any function that's not called with a "new" keyword and returns a new object.
By using this function, we're not duplicating code, relying on prototypes or inheritance. We have the ability to change these types pretty easily. A good rule of thumb is object literals for one, and factories for many.
Now that we have this factory function, this can act somewhat similar to an interface. We can make sure that every character has a property of smart by giving it a default parameter of true.
If we place it inside of our object, we'll always have it on our users. By placing it above the spreading of our character attributes allows the character object that's passed through to override this smart property.
To recap, factory functions are just functions that return objects and are not called by the "new" keyword. They provide a dynamic method for creating multiple objects with similar characteristics.