This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

"Private" Variables with Closures and Factory Functions

3:01 JavaScript lesson by

By making use of closures and lexical scope, we can achieve "private" properties by returning objects with methods from a factory function. By defining our desired "private" variables within our factory function and accessing these variables from within our returned object's methods we create a closure and maintain unique, separate references to our "private" variables.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

By making use of closures and lexical scope, we can achieve "private" properties by returning objects with methods from a factory function. By defining our desired "private" variables within our factory function and accessing these variables from within our returned object's methods we create a closure and maintain unique, separate references to our "private" variables.

Imagine that we have some guests here. We're going to have two people we'll be working with. We'll have Joe. We'll just give him a name of "Joe." And Sally, we'll give her the name of "Sally." We're going to have Joe and Sally greet all of our guests.

Let's define our person function here. This is going to take one parameter of a name. We're just going to build up a person and an object. For now we'll just give it a name. We'll return that. So here we're returning the name of "Joe" and we have our person object. Then we have "Sally" here.

We're going to give them a function "greet guest," which is going to take the guest name, and we're just going to build up this little greeting here, "Hi, guest. My name is 'my name.'" We'll return this greeting.

If we want to greet all of our guests we can do that in our guest's array. For each we will say "Joe greet guest" and we'll bind that to Joe. We'll do the same thing for Sally here. Let's add a break point right here. Here we'll create our objects and greeting, "Hi, Bill. My name is Joe," and so on and so on. We can see in our next set, "My name is Sally," and we go through that list.

Let's add a variable here within our person function. Let's keep track of our "times greeted." We'll start that out at zero. Every time we call "greet guest" we're going to increment that and say, "Hi, Bill. My name is Joe," times "greeted one." We can see that right here, keep running that, we can see that's incrementing.

Now we're going to begin Sally greeting the guests. When she starts, rather than this being incremented to eight, we'll see that she has access to her own "times greeted" variable. That's because it's declared in this function and we're returning this object, and this "greet guest" becomes a closure having access to the "times greeted" variable here. Every time that it's run that is incremented.

We could head over here and we could just have Joe greet the guest of Bill again. We run that. See now we've "times greeted" eight. Let's just log this out to the console to get a clearer picture of what's going on here. If we keep running this we'll just see this log out to the console, "Hi, Jessica, Eric. My name is Joe."

I move into Sally greeting the guests. They each have access to this "times greeted" variable because of this "greet guest" function becomes a closure, which retains access to their own "times greeted" variable.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?