Transcript of the tutorial.
TOP
Okay then.
So we have our user class now, even though it's empty and pretty useless at the minute, but we have
it.
And the first thing we need to do inside this class is to create a constructor function.
Now, a constructor function is the thing that actually constructs our object and sets the properties
on it.
So in the future, when we say something like const user one is equal to a new user, what this does
is call the constructor function inside this class to set all the properties on that new user object.
So let's create one.
First of all, we'll say constructor and this name is important.
It must have this constructor name.
And remember, this is the same shorthand syntax for a function that we used inside objects.
It's exactly the same inside a class.
So inside this constructor, this is where we would set up properties on the object.
Now, before we do that, I just want to talk a little bit more about this new keyword and what it's
actually doing.
So let me just paste these three steps in.
So whenever we use the new keyword to create a new object, it does three things.
First of all, it creates a new empty object like this.
It has no properties at the minute.
It's just a blank object.
Then it binds the value of the this keyword to that new empty object inside the user class or inside
whatever class we're using.
Then finally, it calls the constructor function to build the object inside this class.
So this would be the constructor function.
And inside here now we have access to the this keyword which refers now to the new object that we're
just creating.
So what we could do is attach a property to that new object by saying this dot, whatever the property
name is, for example, username and setting it equal to something.
So I'll say Mario.
And now when we say user one is a new user object, we're creating a new blank object.
Then we're setting the value of this to be the object inside here and then we finally call the user
constructor so that when we call that, we're taking the value this, which is the new object and applying
this property to it, which is now Mario.
So what I'm going to do is create this user and then log it to the console.
So I'll say console dot log, user one and then save this.
And now we can see this object right here with the username of Mario.
So we're creating that user object now and this is all working.
However, what if I want to create a new user?
I could say user two is now equal to a new user.
Again, that's going to create a new object again, it's going to set the value of this to that new
object inside the user constructor and therefore we can attach a username property to it, which is
going to be Mario again.
So if I log out user two this time as well, we can see two objects and they're identical to each other.
They both have a username and the value is Mario in each case.
Now this is fine, but it means every single object that we create using this user class is going to
be identical.
They're all going to have the same property value.
So it would be nice if instead we could pass in a value into this user constructor when we call it right
here.
So for example, I could say Mario and then the name of this one would be Luigi, and then inside the
constructor it would be nice if we could access these and we can do we can pass these through as arguments
here and we can take them as parameters up here.
So I can say username.
And then what would happen when we call this new user is it would take this argument and assign it to
this username parameter.
So then I could say this dot username is equal to that username parameter, which is whatever value
we pass in.
So now the username for this first one should be Mario and the second one.
Luigi.
So let's see that.
Save it and preview.
We can see Mario and Luigi.
So that makes the objects more unique and we can pass more than one argument in and take more than one
parameter.
So for example, I could take an email as well and pass that in after the username.
So I'll say Mario at the net ninja.co.uk for this one.
And then for the second one, Luigi at the net ninja.co.uk And now what we're doing is passing in two
arguments.
And by the way, the order is important here, just like regular functions, the first value, the first
argument is going to be assigned to this parameter, the second to this parameter and so forth.
But now what I could do is create another property called email and set that equal to the email parameter
that we take in to the constructor.
So now if we log these out, we should see two properties on each one of these objects.
Awesome.
So now we're creating unique objects using this constructor and passing in values.
And one thing here, whenever we create an object like this, we say that we are creating an instance
of the user class.
The instance refers to the individual object that we actually create using the class.
So I think you're going to agree that this way of creating multiple user objects is much, much easier
than creating them all manually using object literal notation.