Check out a free preview of the full The Hard Parts of Object Oriented JavaScript course:
The "new & this Keywords" 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 introduces the `new` keyword and what it automates in object creation, but also how it instantiates the need to refer to it.

Get Unlimited Access Now

Transcript from the "new & this Keywords" Lesson

[00:00:00]
>> Will: Introducing the keyword that automates all the hard work. The new keyword. We're gonna call, we're gonna run our function with a new keyword in front of it, inserted. And that people is going to automate. So we're gonna insert a new keyword in here. Folks, that's gonna automate three major things.

[00:00:25] It's gonna make our object for us. No declaring it using object.create, no no no no. It's gonna make it for us. We're not gonna have to return the object out ourselves, it's just gonna return it out automatically. The object it made. We're not, hm. So that's saving us some work.

[00:00:43] It's also creating some challenges. Now we need to adjust how we write the body of userCreator. For example, how can we refer to this auto created object, before the object we created was done manually by us? We referred to it, to add properties to it using newUser. We declared an object, newUser, and we referred to it as newUser.

[00:01:06] But if we're not gonna write that line of code, and instead it's gonna kinda be automatically inserted for us, how are we gonna refer to it? JavaScript gives us a keyword to refer to it, Michael do you know what it is?
>> Michael: Constructor.
>> Chris: This.
>> Will: Spot on Chris, the keyword this, exactly.

[00:01:25] But also there's another challenge. Whereas before, when we declared that object, we wanted that object not to require itself to have the functions like increment and login, but to have a link to them through what hidden property, David?
>> David: Proto.
>> Will: Through proto. But now we don't get to make that bond ourselves.

[00:01:48] That object's being made automatically and we wanna save ourselves making that bond. So where the hell are we gonna and, how are we gonna make that bond to a set of shared functions? And not only that, that set of shared functions, that Object of shared functions. How are we gonna make a bond to it?

[00:02:05] Or alternatively, is there some place that we can put those functions such that JavaScript will know to automatically make the bond to them? Is there some special place? So rather than us declare an object. With a label and make the bond to that labeled object, instead java should gives us a place where we can put those functions.

[00:02:32] The all of the objects that get returned from running user creator with the help of this new keyword will automatically get a bond to this set of functions. But where can we put them do we need to maybe create an object called? I don't know like same name as a function user creator and then functions or something, I don't know.

[00:02:55] Where is it? How we're gonna do it? Now, we need to adjust how to write the body user creator, having regular grid object, no problem, keyword this. But how can we know where to put our single copies of the functions?