Transcript from the "OOP Exercise, Part 2" Lesson
>> Will Sentance: What alternative techniques do we have for creating objects? So we're gonna see all sorts of sophisticated ways. But what about just declaring an object? What alternative way could I declare an object? Andrea? Just declaring an object?
>> Andrea: Curly brackets? Empty [CROSSTALK]
>> Will Sentance: An empty object and then fill it in with dot notation.
[00:00:19] Yes spot on, using dot notation. We're just gonna encounter all of these quickly to make sure we know, and the next one's gonna introduce us to a very special approach to creating objects. So this one, what's happening? Shelby, what's happening in line one, Shelby?
>> Shelby: User2 is an empty object?
>> Will Sentance: Yep, and then what do we do with it?
>> Will Sentance: Shelby?
>> Shelby: Fill it with name Tim.
>> Will Sentance: Yep, we add the property, assign the property, name the value of Tim. Sound good? How would you say that to score?
>> Shelby: Assign the score property of 6.
>> Will Sentance: Yes, perfect.
[00:01:03] And then increment method, and there it is. Good, and we could also call user2.increment and would reach the guy here. Good, all right, a quick note, we didn't use square bracket notation. We only use square bracket notation in one condition. What is that condition? Griffin?
>> Griffin: I'm gonna use an array, no?
>> Will Sentance: No, for object assignment, for object property assignment. That's a fair point, for arrays as well. But for assigning stuff to objects, or accessing stuff in objects, only one condition, Andrew?
>> Andrew: A name is a variable?
>> Will Sentance: It's a variable. We don't yet know what it's gonna be until evaluation time, until the run time.
[00:01:41] Cuz then we couldn't put dot because, suppose we had future score, dot future score. It's going to look for a property dot future score. Whereas what we want to do is fill in that future score and evaluate to an actual value later on. We use square bracket notation for that.
[00:01:57] You never use square brackets with quotes. It's just non-standard. Always dot notation by default. All right, anyone know a final way of declaring, another very interesting way of declaring an object? You remember my little quiz I sent around at the start. I did shout out this way. Katie?
>> Katie: Is it the object prototype is a create?
>> Will Sentance: Not a prototype, but the object has a dot create method on it. She's right. We can use this built-in function object.create. Now we're gonna use it in a very boring way here. But it is gonna turn out, this is going to give us a very powerful set of bonus features we can use later on.
[00:02:43] So remember our goal here the whole time is I want to build an application, and I want to model my data and functionality within it in a way that's easy to to reason about. So my application has users and functionality. I've decided the best way to capture that is to wrap up each user as an object.
[00:03:04] We're seeing user1, 2, and 3 and to have the relevant functionality that could ever be applied to that object, that user data, immediately there on the object. That's my goal throughout today. So what are we doing here in line one? I'm not gonna expect you to guess. I'm just gonna tell you that in line one here, this actually.
[00:03:26] Well what do we think it's doing? What do you think, Shelby? What do you think this line one is doing? What's the left-hand side doing first? You can do that.
>> Shelby: Labeling a variable user3.
>> Will Sentance: Sure, and what's the right-hand side gonna leave us with, do you think, Shelby?
>> Shelby: Creating an empty object.
>> Will Sentance: She's right. It creates an empty object, good job. Empty object, note by the way, so we're gonna add something. The null is not the reason this creates an empty object. We're gonna see later on, I don't mind what you've passed in there, it ain't gonna affect the fact this is an empty object.
[00:04:04] If we pass something else in, we get some bonus hidden stuff. We'll see that later on, but this line, object.create, creates an empty object regardless. Okay, so we have an empty object called user3, Shelby, what's the next line say?
>> Shelby: Assigning the property name Eva.
>> Will Sentance: Eva, Art, next line.
>> Art: We're assigning visit property score with a value of 9.
>> Will Sentance: Good, and then the final line, Griffin.
>> Griffin: It's gonna be assigning the function [INAUDIBLE].
>> Will Sentance: With the name of the increment.
>> Griffin: [INAUDIBLE]
>> Will Sentance: Good!