0:08
So now, we've talked about how objects work and how we create them,
and the function keyword and how the first class functions work.
And we're going to talk really about how we create and use and discard objects.
A basic OO concept is the concept of a constructor,
and the constructor is the code that runs at
the moment the object is being created from the class.
Okay? Destructors are the moment that the data in the object is being thrown away.
Now, JavaScript doesn't have destructors.
That's because everything is quite dynamic.
Destructors are unpredictable in languages like Java.
I think C++ there are more predictable when a lang goes out of scope.
PHP destructors are more predictable but, okay.
The conceptual idea is a constructor's code that you can jack into
the moment of creation and destructors are
code that you can jack into the moment of destruction.
So if we go back to the PHP,
you'll see a better example of how destructors work,
that has to do with how the PHP run time
ultimately works because the request-response cycle.
But, constructors are the most important.
Destructors are of secondary importance.
So, in JavaScript, the constructor is implicit, right?
Because we're using sort of the essence of first class functions here,
the constructor is all of this code.
And it really is running to define the shape of the class.
There's a variable X in the class.
There's a function called party in the class.
But it's also just running code.
So you see, when this new happens,
it actually runs all this code. Now, it doesn't run this code.
It runs the code to create that,
the console log in the constructor,
and then this.party gets constructed, and that's the new.
That's what happens at the moment of new.
It doesn't run this code but it remembers this code and
assigns it into this.party for that.
And then, of course, we do the an.party and away we go.
And so that the rest of that is exactly the same
as the previous example that I showed you.
So the constructor is the function itself as compared
to a specially named function which in the other kind of object-oriented pattern.
It's an often especially named function.
So we can create many instances, many objects, classes.
There's only one of those but that's a cookie cutter.
We go stamp, stamp, stamp, and then object, object, object, right?
And so, each one has its own copy of instance variables.
So here's an example of that, right?
We're going to actually create two instances from one class,
two objects from one class and work with them.
So the other thing that we're going to see in here is we're going to
see the idea of a constructor parameter.
And so, this is a parameter that we're going to pass in
and the new is the thing that causes the constructor to run,
causes all this code to run,
but we can pass a variable in, right?
And so we can pass in different moments of construction.
We can pass different ones in.
So the first time, we're going to construct a variable with Sally in
it and we're going to copy this constructor variable into an instance variable.
So this is an instance variable,
instance variable, this is a method.
And so this nam is going in there.
And the interesting thing is, this is code.
So, look at this nam.
This nam is coming down from here.
So that this.party is actually different code in
the s variable than the j variable because it's looking this nam,
it's sort of instance differently inside of it.
It's kind of amazing how that works, right?
So, it runs, it creates three things inside here,
and then it gives this back and then points s at it, okay?
And then we call. So that says built Sally.
This little message says built Sally.
And then we call the party method.
And you see, it knows Sally.
It runs this code, adds one to it,
prints out the one and the Sally,
so that prints out there.
Then, we're going to do this again.
We're going to run the constructor again and nam this time is Jim.
So Jim goes in there and goes in there and goes in there.
So then, we end up with a new object that is got Jim in it,
and another data in it.
And then, when this finishes,
it returns this object and we store that into j.
So we have now, two instances.
The first instance is in a variable s,
the second instance is in a variable j.
And so now we call j.party and so that runs
this party code except that it runs it with Jim equals one,
and s stills are there.
So we'll call s.party and then we'll go run the code again, except that,
the Sally instance and the variable x is two and so that runs there.
So we have a situation where we have two instances,
two variables, and we're working with them independently.
And that's really kind of the beauty of object-orientation.
As you create this one template,
one class, and then you go stamp,
stamp, and you have two objects and they function independently.
They have their own variables,
their own code, and away they go.
So, what we've been talking about is we make this template in a class
that consists of methods or messages and attributes,
and then the constructor is the place where we set everything up,
and the object or instances,
what we get back from the constructor.
So, this was a really quick and short lecture
because I think you know a lot about object-orientation in general already.
And so, all we really wanted to do was talk about
the real special sauce that JavaScript has.
And, JavaScript's objects are dynamically extendable.
You'll notice when we said this.x,
we didn't have to tell it in advance we are going to do that.
Actually, executable code creates the new attributes and at run time,
you can actually create new methods as well.
And so, these objects are powerful and if we go all the way back to arrays,
we kind of, without even knowing it,
you can use objects in JavaScript.
You can say curly brace,
key value, key value, key value,
sets up an object, in that case,
with no methods but with a set of attributes.
And so, we kind of treat objects as JavaScript's equivalent of an associative array.
So, up next, we're going to talk about jQuery which is probably
the most powerful object that most of us will use in JavaScript as we write code.