This course has been updated! We now recommend you take the JavaScript: The Hard Parts, v2 course.

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

Will works through the OPP code example to show encapsulation, which is the binding together data and functions that manipulate that data.

Get Unlimited Access Now

Transcript from the "OOP Exercise, Part 1" Lesson

>> Will: We are gonna map through this folk, line by line as we begin to go through OOP. Andrew, what are we doing?
>> Andrew: Defining user one as that object.
>> Will: As that object, excellent, there it is.
>> Will: Okay, now, what properties are we storing on it? Andrew, sorry Katie.

>> Andrew: Name scoring increment.
>> Will: Yep, and what's the value in each? I'm really just dragging this out right now, Andrew. So help me out.
>> Andrew: [LAUGH] Well, three and that function.
>> Will: Well, what do we say? Andrew, you're right. Yes, we've declared the user1 object, there it is.

[00:00:42] Draw objects like this with name, will score 3 and increment is what, Andrew?
>> Andrew: That function.
>> Will: That function definition, and we call them methods, don't we, when they're on object. Is it being called at this point? No, we just defined it, associated the anonymous function value, the function value with the property increment.

[00:01:09] And now we hit the next line of code. Andrew, what's the next line of code in the global execution context tell us to do?
>> Andrew: Increment user 1.
>> Will: Yeah, let's be really precise here. I want to be really precise here. Firstly what does it, when it sees this line, what's the first thing JavaScript has to work out, Andrew?

>> Andrew: Get that object from.
>> Will: User 1 object, exactly. Gotta go look for it and see does this guy exist? Yep, there is it. And then does this increment property exist? There it is. And does the function code, it's a function, great. So I can start creating everyone together, a new?

>> Speaker 3: Execution context.
>> Will: Excellent, beautiful such rich mellifluous tones. Okay, indeed. So in we go.
>> Will: Call stack gets user1.increment on the top of it, global, just below. And we have our local memory, okay. Now, we know we got the increment code from user1 function and in here It says user1.score++.

[00:02:25] So there's nothing being declared in local. Is user1.score++ is user1 here in local? No it's not, so where do we look for user1 in global, cuz it's next on the call stack? And what do we find? We find score and we increment it to 4. Perfect, our fundamental goal of running a method directly on our data encapsulated in an object so it's all bundled together in one place.

[00:02:56] Which makes our lives so easy. Cuz I can move this object use the one anywhere in my code, and know the functionalities is gonna be available right on it. This is wonderful for writing code scale. We've achieved it here.