Check out a free preview of the full JavaScript: The Hard Parts, v2 course:
The "Factory Functions" Lesson is part of the full, JavaScript: The Hard Parts, v2 course featured in this preview video. Here's what you'd learn in this lesson:

The built-in function Object.create() is introduced as a way to create null objects that provide more control on the object. The concept of the generalized function to produce objects is introduced.

Get Unlimited Access Now

Transcript from the "Factory Functions" Lesson

>> Will Sentance: What's another way that I can, what's a built-in function of JavaScript whose output will be an empty object forevermore? Yeah, Adam, please?
>> Adam: Object.create.
>> Will Sentance: Object.create, very nice, from Adam, yeah, spot-on. User3 using object.create. This is a built-in function in JavaScript. It's gonna give us later on some fine-grained, it's such a nerdy word.

[00:00:27] Give us some fine-grained control over our object. But I just want you all to know for now, it's only thing in terms of our object itself, that it does, is return out an empty object. So right hand side here. Well let's just declare left hand side here first.

[00:00:48] Over to you Kayla. Hold on, why do I always skip the last letter? It's very interesting, right? It must be me trying to accelerate my writing. All right, yeah. Left-hand side first Kyla.
>> Kayla: We're declaring a constant User3.
>> Will Sentance: Yeah, user3, excellent. Right-hand side is gonna immediately return fro us what Kyla?

>> Kayla: An empty object.
>> Will Sentance: An empty object. That is it people. And so even if I were to,
>> Will Sentance: Even if I were to pass into those parenthesis, you might think those parenthesis having null in is why it's empty. I could pass in an object in there full of objects full of properties, I don't matter what I pass it, I could pass in a num anything I want.

[00:01:32] It's going to return out an empty object. Now that empty object might have some hidden properties on depending on what we pass in, or hidden property on. But it's always an empty object, no direct properties on it. We then assign through to it name property, which is the value.

[00:01:53] Eva school which has stored in it. 9 and again increment method which is a function. Let's just confirm whether I can do my own important thing. Use3.increment, Todd can I assist you? My first time ever was again and again and he's my green from I look up here.

[00:02:24] Yeah green for lookup. Where do I look for user3, Todd?
>> Todd: In the Object memory.
>> Will Sentance: Evo memory, isn't it?
>> Todd: Yes.
>> Will Sentance: Why do I look for increments?
>> Todd: In the user3 object.
>> Will Sentance: And do I find it.
>> Todd: Yes.
>> Will Sentance: If I do. Grab its code and execute it be was a dream come true.

[00:02:41] It's a dream come true. I done this is an object oriented. I've got data and functions, data methods that work together and don't work with other stuff. Now, strictly I could always use these function in other areas. So it's not strictly kind of protected but for general purposes, we're pretty much good to go.

[00:03:01] But our code is getting repetitive, what principles are we breaking Ryan?
>> Ryan: DRY.
>> Will Sentance: DRY, don't repeat yourself, we're hand crafting these objects. Every time a user logs in, we're gonna have to write out fully the developers behind the scenes filling out. Their properties. Whenever we have code that is being done multiple times, like adding properties.

[00:03:23] What do we tend to do whenever we're doing lines of code, again and again and again? What do we wrap them up in, Todd?
>> Todd: I would generalize function, abstract it, down.
>> Will Sentance: Put in a function. Save it once use again and again. And the only bits you want to change have those be passed as inputs to specify when you run the function, what it's actually going to do.

[00:03:42] Yes but on Todd is exactly what we're gonna do. And folks solution one for and I just wanna drill one more time in case people aren't really really feeling this. It is a dream come true for me when I'm writing with 100,000 lines of code that when I say my data.

[00:03:55] There's no mystery as to what my functionality is.
>> Will Sentance: Well, what my functionality is that will apply to that data because look at that folk, it's right there. The function that could be applied to that data. And once you start thinking in terms of this paradigm, of putting your data and the functionality together in one place.

[00:04:21] All the functionality I could ever need for the user3 is right there. Once you start thinking in that way, you start doing this a lot. You start thinking of everything in your applications as what's the data in the functionality connected to that particular feature? It's actually really, really nice.

[00:04:36] It's not as popular as why I'm really emphasizing this is about understanding the principles in JavaScript. Let us do this, which we use all over the place. Because the paradigm of object oriented programming is not as popular increasingly as the functional programming style, but are amazing, really intuitive.

[00:04:52] Way of thinking about structuring an application, application is data. User scores, whatever functionality, the ability to change that user score, put it together. And look at it, is that right there. Beautiful, but we're doing a mobile time. So now let's do the work of creating the object, save it once.

[00:05:15] Create the objects and then use it as many times as we like. That's exactly what we're gonna do here. Generate our objects, generate isn't even the right word. Create and return our objects using a function. This solution one people going to turn out to be untenable. You can never use it in practice, but it gets us a long way there.

[00:05:36] Everything else we do it's just about making this much, much more efficient. I have given you a hint there.