This course has been updated! We now recommend you take the ES6: The Right Parts course.
Transcript from the "Private Properties" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Aaron Frost: I don't want people to access my monsters health property, like manual, I don't want them to be able to say, what's the monster's health. So I want to have like this private property called health, and if you want it, you got to call get health. You can't just say like give me the health.
[00:00:23] I don't want you to be able to manipulate it because if you adjust the health I need to be able to react to that internally right. And so I'm going to make so by saying this sub monster health equals health. If you pass in the in the health in the constructor, it's going to sign like some GUID on the this equal to health.
[00:00:51] So the reason it's private is because you're not going to be able to predictably know what property belongs to what, you know what I'm saying, does that make sense? And so especially, like if you take this code and over in the REPL.
>> Aaron Frost: So if you take this and you say, var a = new Monster
[00:01:25]
>> Aaron Frost: What's our monster's name? Kevin, of course dude. All right his hit point is one. Kevin's not a very powerful monster.
>> Aaron Frost: Come on Kevin
>> Aaron Frost: Why is it aligning my style? Dude, is my browser broken? How come none of my stuff's working?
>> Mark: But it works in [INAUDIBLE]
[00:02:13]
>> Aaron Frost: Dude, it's totally so not even working.
>> Aaron Frost: There we go. My man's got, he's got my stuff cranked up so high, I can't even see it. That was you, Mark.
>> Mark: Always the font size.
>> Mark: Just [INAUDIBLE]
>> Aaron Frost: Yeah. Let about var, because we're going to put in a closure anyway.
[00:02:50] There we go. Now it logged the monster. So, this monster, and his name's Kevin, and he's got some random property, that's got a one in it. Okay. And the only people that know what that property is, is us because when we declare a function and we need a private property like this, were to put it inside of a closure.
[00:03:23] So, that we're the only ones that have access. We're the only ones that have access to that symbol and we don't want the world to be able to gain access to that symbol, right? Otherwise everyone's going to know what it is, they're going to manipulate or stuff. So, we just go ahead and we wrap like that in a runtime.
[00:03:41] There's no certifiable way to detect what's going on here. So if I say this.monster.
>> Aaron Frost: So I should pull this back out.
>> Aaron Frost: Okay, I got it. Sorry that was so painful. So, this way everyone can see what monsters they can all constructed but they can't see, they're not gonna be able to know what my private variables are.
[00:05:18] So, like, if I had health and then maybe, what's another feature of a monster?
>> Speaker 3: Horns.
>> Aaron Frost: Horns? Right?
>> Speaker 4: Attack value.
>> Aaron Frost: Attack value?
>> Speaker 5: [INAUDIBLE] Speed?
>> Aaron Frost: Speed.
>> Speaker 5: Line movement.
>> Aaron Frost: Yeah yeah. Speed is good. I don't want other people like to be able to mess with my guy's speed, right?
[00:05:55] Like, they can maybe find out what it is, but I don't want them to be able to alter the value of that property. So that's kind of a thing that I would definitely want to use this for too, that was a good call. So, when you do the sub, you could be like speed, he does speed, this[monster.
[00:06:20]
>> Aaron Frost: Right, and then if we need to get the speed out we can say, get speed. So we can use one of the ES5 getters or properties I'm gonna say, return this[monsterSpeed, right. So now over here, I've gotta say a dot, I didn't pass it in. Who's Kevin?
[00:06:56] Kevin? Is there a Kevin in here? Not a real person. I thought you're trolling someone. But it's not even a real person. Who said Kevin? Is it a real guy?
>> Speaker 6: No.
>> Aaron Frost: Just made him up.
>> Speaker 7: There's a Kevin in the chat room.
>> Aaron Frost: Kevin, take that buddy.
[00:07:12]
>> Students: [LAUGH]
>> Aaron Frost: Kevin is like dude, the monster's strength is a million.
>> Students: [LAUGH]
>> Aaron Frost: No, it's not, buddy, it's not a million, it's negative one, the speed. Anyway, so, that's how you implement a private property are you guys in love with that? You hate it, you love it, hate it.
[00:07:31] Hate, love, I don't mind much, either way. Who's just happy that they got something out? I'm just happy that I got something out.
>> Speaker 9: [INAUDIBLE] they made a way to do private property.
>> Aaron Frost: If they agreed on something, GUID generator, and if they agreed on this class syntax, and we could combine the two to get it private-ish property, even though if it's only private by obscurity That's fine with me.
[00:07:55] I don't care.
>> Speaker 9: In chat, there's a question, if you could just explain symbol really quickly.
>> Aaron Frost: Symbol? I actually haven't read the spec. I only know that this is what we're going to use it for. But let's go read it real quick.
>> Aaron Frost: Actually it's more, on and pull the other site.
[00:08:19] That was a good question. Harmony proposals, tone that back up a little bit. It's not even on there that's what I thought I never saw it on here. So if you go to symbols over here
>> Mark: There it is.
>> Aaron Frost: Yeah. Take you over here and it is super easy to understand.
[00:08:49] Okay, the symbol constructor, is what the [INAUDIBLE]
>> [INAUDIBLE]
>> Aaron Frost: The symbol constructor is the %Symbol% intrinsic object of the initial value of symbol property of the global object Totally. That's what we thought it was right? I don't even know what that means?
>> Speaker 10: Perfectly clear now.
>> Aaron Frost: Yeah.
[00:09:27] When a symbol is called as a function rather than as a constructor it returns a new symbol value. That's counterintuitive. When you call without the new, you get a new symbol value, it's cool, whatever. The symbol constructor is not intended to be used with a new operator. Okay, that's fine.
[00:09:47] Or to be subclassed. It may be used as the value of an extends clause of the class declaration. But a super call to the symbol constructor will not instantiate any subclass instances. Anyone? Did that answer no one's questions? Yeah, I think that answered nobody's question. But that's what it is, because it's not on the proposal site.
[00:10:08] So sorry to disappoint. For now I think of symbol as like a GUID generator. That's never gonna give you the same one twice. Can we agree on that? At least for the purposes of classes?
>> Speaker 9: So when you use it for a property name, I feel like I read that it's treated specially, like it doesn't return a string so much as like it makes it not a new mobile property it's well that's sounds great to you.
[00:10:40] It's like if I create a Monster and just like, there's properties the monster health does not show up on the list of property names I think?
>> Aaron Frost: Maybe. I don't know. I don't think so. Okay. Yeah, yeah, yeah, yeah. So ,if I say this.getOwnProperty. Yeah it won't be able to find it.
[00:11:03] Like so you know on an object you can say, object.getOwnProperties? And it says hey, well, I've got a name property, and I got one of these, and then I got one of these, right? I got a speed and health. Well, if you use this symbol supposedly, it's not gonna show up on the getOwnProperty list.
[00:11:25] So, people won't even be able to like poke the thing, and they would be like, hey what keys are on you, like they won't know. So that if that makes any sense.