Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features
Autoplay

    Create Factory Functions for Object Composition

    Tyler ClarkTyler Clark
    javascriptJavaScript

    Factory functions can be powerful ways of abstracting away difficult inheritance situations. They are simply functions that return objects in a desired state. Let’s refactor some objects that share duplicated code to a reusable factory function.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 00:01 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.

    00:09 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".

    00:18 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.

    00:35 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.

    00:49 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.

    01:03 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.

    01:32 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.

    01:55 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.

    02:11 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.

    02:22 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.

    02:36 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.

    Discuss

    Discuss