prototype parameter, which is also an object. The second unique thing is that you can use the
new keyword on a function-object to create a “child” of that object.
Prototype gives us some special abilities when creating child objects out of function-objects. When you add keys to the prototype parameter they automatically get added on to the child object. That means that you can define class-level functions like
bark(); that will get applied to every new child object.
Just as a note, the keys on prototype can be of any type. It’s commonly used for functions, but you can set it as a string, number, function, or even another object.
You’re probably wondering why in the world you would want to use prototype, rather than just adding the keys in the constructor. Prototype creates some spaghetti code - it would be much easier just to do this:
You’re right. That would work just as well as the example above, but it’s pretty short sighted. The way the above works is that everytime you create a new Dog, it runs the constructor function and instantiates new functions on the desired keys. Not only is this inefficient processing-wise and memory-wise, it also severely limits your inheritance and abstraction abilities.
Consider a different example - let’s say you are writing a game about working at a zoo - naturally you’re going to have some
Animal objects. One of the most interesting things about going to the zoo is the noises each animal makes, so we defintely want to have a
speak function on
Animal. Now, what noise does an animal make? Certainly it should change depending on if it’s a monkey or a polar bear.
In order to do the abstraction on the
Animal class, we have to store the
speak function on
Animal’s prototype, and then override it on each child class. Note that a generic
Animal won’t make any noise, so that’s what the
Animal.speak function will do.
If we had instead set all of the functions in the
Animal constructor, we would have had to rewrite the
speak function for the
Sloth, even though sloths don’t make any noise. This may seem like a negligible amount of work, but I hope you aspire to do more interesting work than programming
Sloths all day.
I’ve been using a pattern recently to mock standard Object Interfacing. Of course, the code isn’t quite as clean as a language that supports interfacing out of the box, but it gets the job done.