Check out a free preview of the full The Hard Parts of Object Oriented JavaScript course

The "Creating an Object" Lesson is part of the full, The Hard Parts of Object Oriented JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Will starts by establishing objects as the best method to encapsulate data and functionality.


Transcript from the "Creating an Object" Lesson

>> Will Sentance: Let's suppose that we're building a quiz game with users. Let's see our example data. We've got a user here with a name property Phil. It's a name characteristic Phil. A score characteristic 4. Another user with name Julia, a score and the ability for either of these users to have their score increased.

What would be the best way to store this data functionality together, Michael?
>> Michael: Within an object.
>> Will Sentance: In an object, beautiful. Because objects allow us to store data with their associated functionality all bundled up on an object. There it is. And this is going to enable us and everything we do today as we lay out more and more sophisticated approaches to structuring our code, orienting it around objects so that the heart is always gonna come back to can I do this thing here?

Cuz this is gonna make my life effortless. If I can run a function on the right-hand side of a dot, that is to say, apply it to an object and know that it's available on that object it, I can apply my functionality to my associated, to my relevant, to my pertinent data, no problem.

That is my dream. Throughout the whole session today we are just gonna go again and again and check. Can I run my appropriate, my pertinent functionality on my pertinent data? Are they bundled up? Not go hunting for the increment function, not go hunting for the function that's relevant to my User1 data, but know that it's right there on my object itself.

That is my dream. Well, can we do it here? Well, yeah, absolutely. User1, and there's an increment property, and increment method on it. We call it, and it's going to change User1's data. This is known as encapsulation. JavaScript doesn't implement this very, very, in a very strict way.

But the essence is put the appropriate functionality with the appropriate data, and the ones that are related to each other, and bundle them up into what we call an object. All right, let’s keep creating our objects. But first, note briefly that we would have more functions than just this.

We’d have ability to decrease score, delete user, login. We’d have a ton of functions. And ideally, they're all gonna be available, userone.decreasescore, userone.login, userone.logout. Folk, this will make our life writing code so much easier. All right, let's create objects using a few other approaches just to get ourselves in the mindset of how we would go about creating these objects that in any application each user is gonna be represented by a different object with its data and its associated functionality bundled up on that object.

Think User2 might be, I don't know, well, let's see.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now