Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

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

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    "Private" Variables with Closures and Factory Functions

    Erik AybarErik Aybar

    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.

    javascriptJavaScript
    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

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

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

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

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

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

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

    02:24 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."

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

    Discuss

    Discuss