Transcript from the "new operator" Lesson
>> Will Sentance: In the challenges, we started to encounter this magic keyword, new. So we're gonna remind ourselves that we wanted a function, that when called, when run, would return out little baby objects, user1. Call them, first one user1, next one, user2, user5,000, user whatever. But we wanna have available on each of the objects our functionality.
[00:00:24] Have I mentioned this yet? I know, I mentioned this many times. This is our fundamental goal. Can I call this function on the object, is it available right there on the object. That's all I want. But I don't wanna have multiple copies of this function on user1, user2, user5000.
[00:00:39] So along comes this special shed store, of the functions that I could ever want on user1 user, it wouldn't just be increment login, it would increment login, logout and change avatar, display avatar, etc, etc. All those functions on user function store would be available when I call user1.thatfunction.
>> Will Sentance: All these prototype bonds. I hate putting away from the pairing, I'm sorry.
[00:01:58] I feel very bad, the pairing is where the real value's at, it really is. But right now we're gonna see our solution three in a moment. We're gonna get, let's see our review with this approach problem. No problems, this a beautiful approach. I love this approach maybe a little long-winded.
[00:02:14] We've declared a new variable with an object associated with a bond to function store. We've returned that object. Six words it's not that bad but, I will say this is super sophisticated but it is none standard. This is a non standard way of creating our objects with shared functionality.
[00:02:36] This is a non standard way, we have a standardized way. In the down side of this approach yes six was but also, once you start creating objects in which you have your data and functionality, you start doing this for everything. Are you creating a quiz game you generate a quiz board as an object with functionality attached to it.
[00:02:56] It's a single instance the of the quiz. You're not gonna create more of them. You just start thinking OOP for everything and therefore if you are manually doing this inside each time. Yeah, may be it is not as, may be there is a better way. Another language you are not doing this, if you are trying to achieve oop.
[00:03:33] Not magic, I don't know why magic there. This keyword new. When we run this user created function, with the new keyword in front of it, it behaves in a totally under the hood, similar way to this, not identical, but similar. But it's gonna automate so much of the hard work we did here, declaring the and so forth.
[00:03:57] Let's see. When we call the constructor function, this guy here is known as our constructor function, with new in front, we automate two things. We automatically create the new user object. We automatically return out that user object when complete and store in user1 for example. Who can see why we have to rethink how we write this code then?
[00:04:19] Cuz right now, I declare an object code new user, it's an empty object, and then to fill in the properties name, how do I refer to that object, Katie? What label do I use to refer to the object to fill in the name property?
>> Katie: New user.
[00:04:42] What's the label for my object? That label's gone, that's automatically done for me. The object is created for me by Java, everything in red is stuff that the new keyword when I run the user created function, instead of rekali but instead with a new keyword in front of it.
[00:05:17] What might be a useful place holder keyword or word that we could use to point to that object, Katie? What do you think might be an useful word, Griffin?
>> Griffin: This.
[00:06:00] So who can now see another problem? We now know how you can refer to our object, but who can see another problem but this automation by automating this line, by automatically creating the object, what's another problem here that someone can see?
>> Speaker 4: There's still a bond.
>> Will Sentance: How do we get that bond?
[00:06:16] Cuz before, we manually made that bond, didn't we? We said object.create parentheses user function store. Now, we don't get a chance to make that bond. That's gonna be automatically done for us. The bond here is also gonna be automated. But if it's automated, what's it gonna refer to?
>> Speaker 5: This.
>> Will Sentance: This is this object here. We want something out here that is gonna be a shared function store.
>> Will Sentance: Something like where do we end up actually storing our shared functions? Well Java is a default place and it's a default place and it chooses the default place for the user creator function of the other functions like quiz question creator, gameboard creator.
[00:07:24] Anyone remember?
>> Group: Objects.
>> Will Sentance: Objects, okay so, if that's the case, user creator is a function. Yeah, no problem. It's a function. There it is. It's a function. So if I treat user creator as a function and put parenths on the end, it runs it's functionality. Hurray but what is user creator also Andrew?
>> Andrew: An object.
>> Will Sentance: An object, it's also an Object. So if I treat user creator as an object, it behaves as an object. If I try and store on user creator, user creator.andrew = 3 that has no consequences on my function. The next line I can still go userCreater ().
[00:08:19] If I treat it like an object, it does its object stuff. If I treat it like a function, it does its function stuff. Who feels that it's a bit weird. Raise your hands if you think that's a bit weird. Feels a bit, no that's odd to me okay.
[00:08:35] At least we're being honest, it is a bit weird. So if I did this line here what would I have in using a creator in this object version, Andrew?
>> Andrew: An Andrew.
>> Will Sentance: Andrew as a property and the value would be-
>> Andrew: 3.
>> Will Sentance: 3 yeah. Now, there is one way I've heard, I think it's quite a smart way of thinking about this.
[00:09:18] And when we do use a creator parenths, what we're really doing is say go run user creator and it's cool property, which is the functionality. And that sounds weird, but that's really what we're doing. Think of it being the functionality as sort of there under a call property, a run me property.
[00:09:39] So if you're thinking how can it be both things at the same time? Well really it's just an object with some functionality can be run. Even though we think of it as a function first, it's really an object first. But we obviously use this functionality far more, but it really is an object first with functionality attached.
[00:09:56] But we always when we add when we do parenths, we speak to it's functionality bit. Okay let's have thumbs on that, cuz it really is important for us to understand this is our final piece of knowledge we need to understand for us to be able to use the new key word effectively let's have thumbs.