Welcome back in the previous video we saw that javascript finds methods and properties on an object
by working up the prototype chain.
We also saw that the prototype property is a great place to put properties and methods that we want
to share amongst all objects created from that constructor function.
Let's take a look at an example and see why placing methods and properties on the prototype object is
very efficient.
In this example we're attaching a method called Say hi onto the object created from the person constructor
function.
Now this will work totally fine.
But let's think a bit more about what we just did.
Every time that a person object is created we have to define this function on that object.
So when we make one million objects from the constructor we're adding the same high property.
One million times that doesn't seem very efficient.
It would be nice if we could just define it once and have it accessible from every object created from
the person constructor and that is exactly what placing methods on the prototype property lets us do
in the example below.
We've refactored our code from above so that we only define the say high function once this code is
much more efficient and makes use of best practices with object oriented programming in javascript.
Now it's your turn.
Create a constructor function for a vehicle.
Every object created from this constructor function should have a make model and year property each
object should also have a property called is running which should be equal to false.
Every object created from the vehicle constructor should have a function called Turn on which changes
the is running property for that object to true every object created from a local constructor should
also have a function called Turn off which changes the is running property to False.
Finally every object created from the vehicle constructor should have a method called honk which returns
the string beep.
Only if the is running property on that object is true.
Think about where each of these properties and methods should be define for the vehicle objects created
from the constructor function and will go over a solution in the next video.
Good luck