
JavaScript: The Hard Parts new operator Q&A
This course has been updated! We now recommend you take the JavaScript: The Hard Parts, v2 course.
Transcript from the "new operator Q&A" Lesson
[00:00:00]
>> Will Sentance: All right, let's have fun. I'm not accepting this. I have clarifications. I'm clear that our functions, when treated like functions, by adding parens on the end of them, behave as functions. If they're treated like an object, use a creator.andrew. What would this return for? What would this evaluate to, Andrea?
[00:00:21]
>> Andrea: It's gonna be an object with Andrew.
>> Will Sentance: Well, but with me retrieving this. So here we've already for the Andrew property. So a user created .andrew, I'd be retrieving three, exactly. Okay, so let's have thumbs on that notion. It's weird. Part of it's is just I need more time to embrace it.
[00:00:41] So Andrew's got a thumb Katie's got a thumb, medium thumb, sort of like a thumb. Art is good to go, Lindsey has a medium thumb. Griffin has one. Griffin, will you kick us off?
>> Griffin: So if you were to use the user creator as a call back function, can you use that user creator object data in that function that you're using-
[00:01:01]
>> Will Sentance: You certainly can, but I'll just make this point.
>> Griffin: Yeah.
>> Will Sentance: That wouldn't standard practice.
>> Griffin: Yeah.
>> Will Sentance: Just cuz it's there, doesn't mean we use it. We use it for this one thing we're gonna talk about. It isn't standard practice to bundle them in that sense.
[00:01:14] They're functions, it is all fundamentally objects but they use as functions, okay? But we can store stuff here. We're gonna see in a moment, we're gonna store something here, something here. We are gonna store something here. All right, everyone got that? Okay, Griffin, go ahead.
>> Griffin: Again, with the memory, could you kind of, instead of using the backpack of the lexicon thing, could you-
[00:01:40]
>> Will Sentance: Lexical scope.
>> Griffin: [LAUGH] Yeah, could you store it into that function's object?
>> Will Sentance: This is where it gets a bit hard with hard parts, is we're seeing all of the underlying things, features of JavaScript. Some of them you can cross-use. This one, the lexical scope is a special, magical property that is not involved in conventional sort of objects like this.
[00:02:05] It's hidden off. You're right, we said functions have a hidden square brackets quote, that's not this guy here. That's a really nice clarification by Griffin. This object definitely is not that scope property, do you remember that guy which held on to state? Live in date, okay. That's an interesting one, man.
[00:02:22] Definitely not. No, stop. Stop it. That's not allowed and doesn't, okay. Lindsay?
>> Lindsay: I don't know. I think I got lost somewhere, but what's the function in user creator right now?
>> Will Sentance: User creator was declared.
>> Lindsay: Maybe C, Mr. Pelting.
>> Will Sentance: We are going to see in a second, but user creator was declared as a function at the top right?
[00:02:47] Whenever you declare a function, by default, just by its' very nature, it's also an object. Okay, just declaring that. Talk me through what we did in line 1 there, Lindsey.
>> Lindsay: Declared the function. User creator.
>> Will Sentance: User creator. We're allowed to draw functions in a box like that?
[00:03:04]
>> Lindsay: Mm-hm.
>> Will Sentance: What do we do in a few lines down in let user1 =.
>> Lindsay: We declared a variable and assigned it. The whatever, the user creator will return.
>> Will Sentance: Great, go run that functionality. And I know I have to run the functionality, why?
>> Lindsay: The prens.
[00:03:24]
>> Will Sentance: The prens, good. But let's not forget, if in the next line I was to write usercreator.andrew, in other words treating usercreator as an object JavaScript is not gonna complain. It's gonna go, yeah, sure, yes, I'm a function, but I'm also an object. You treat me like a function, I run like a function.
[00:03:43] You put something in me like an object, and now these start to become weird things to say. You put something in me like an object, I behave as an object. That is just how it is. Okay, Lindsay, so it is a function. Nothing has changed. But if I were to treat it like an object: usercreator and said usercreator.andrew=3, then, if I had to console log my usercreator, I'd still see the function.
[00:04:11] But if I had to console log my user creator with a special Chrome dev tool, down arrow, I would see the function, but also in the properties, I would see some other properties, and our andrew : 3. Okay, so this is not surface level, really. We're not gonna be doing this day-to-day, but it's there.
[00:04:30] This is our day to day. This is what the function really is but we can't store some stuff here but we're not gonna be storing it ourselves. JavaScript stores stuff here for us. That's really what the real story is here. We created a function. But JavaScript can store some stuff here for, we're definitely never storing Andrea three ever ever ever on this object let's be really clear on that, we're not doing that ever.
[00:04:53] We definitely, but JavaScript can store stuff there for us because it has special goals and things it wants to achieve that we wanna make available to you as a creator.