Now let's go ahead and make another constructor.
So this time, I'm going to create a Dalmatian class....
And it's going to be a dog with the color...
[speaking while typing] "white with black spots."
But obviously, I haven't...
created any kind of relationship between "Dalmation"
and "dog" just yet, so if I reload--
oh! Syntax error.
Let's see where that is....
Oh, I forgot my parentheses.
This is a function.
So obviously, if I set...
[speaking while typing] "spot"
to "new...
"Dalmation," Dal-ma-tion....
Let's try that again.
So now Spot is a dog,
but only the color property is set.
So I can say "spot.color," and that works fine,
but if I say "spot.speak,"
that doesn't work at all.
So, in order to define the inheritance relationship,
I need to do something a little odd.
So let me give you a little background first.
So, if you're used to a language
like C++ or Java or C#,
then the way that you define an inheritance relationship
is you have a base class, like dog,
and then you extend that base class
by creating a subclass, like Dalmation.
So...
the way you do it in JavaScript
is a little different.
So JavaScript is known as a prototypal
or "proto-tip-pal" language,
where inheritance is based on prototypes.
So what is a prototype?
A prototype is an object.
So the way you define an inheritance relationship
is you set a base prototype, a base object,
that the sub-object that you're creating
inherits all the properties from.
So instead of extending a class,
which is a blueprint for a set of objects,
instead, you're extending an object
by adding additional methods and additional properties
to the base set of objects and properties
that that object has.
So what I want to do, basically,
is I want to create a new dog,
which is the prototype for all Dalmations,
and then I want to inherit
that prototype
for all Dalmations that I create.
The way that works is the constructor,
the Dalmation function,
has a prototype property, which is an object.
So whenever you create a function,
there's going to be a prototype property
for that function; let's take a look at it.
So Dalmation,
which is a function.prototype,
is an object
with a constructor function.
And actually, this constructor function,
if I open this up,
the function is Dalmation.
So an object whose function, constructor function,
is a Dalmation,
is the prototype
for all Dalmation objects.
And there aren't any other properties
that are really interesting or useful on this object,
because I haven't set any.
But for example,
if I set
[speaking while typing] "Dalmation
".prototype
".legs
"= 4...."
And then I said,
[speaking while typing] "spot.legs,"
all of a sudden, Spot, an existing Dalmation,
inherits the number of legs
from the prototype of the Dalmatian class.
So I can do this for other properties as well.
So dog.sound is "bark,"
"spot.sound" is now "bark" as well.
And this will be true for any other dog that I create.
So for example,
[speaking while typing] Rover is "new Dalmation" as well.
"rover.legs" is "4",
"rover.sound" is "bark,"
but I still haven't set a property...
for speak, so that's still going to be an error.
So this suggests a way of doing inheritance,
which is to make sure that all of the properties
defined for dog are also set
for the prototype object for the Dalmatian class.
And there's a really easy way of doing that,
which is...
[speaking while typing] "Dalmation.prototype
"= new Dog."
So Dalmation is going to have a prototype object
because it's a function,
but I'm overriding that default generic object
with a new dog.
So any new dog that I create
is going to have all the properties
that are defined for dog.
So let's go ahead and-- actually let me save this...
Save...
and then reload.
And now, I'm going to create
[speaking while typing] Spot is new Dalmation.