Understand JavaScript Prototypes

JavaScript Prototypes

When you talk about prototype, you're talking about inheritance. the inheritance of what? not the money of course, but the inheritance of properties and methods, so in JavaScript, all objects inherit properties and methods from a prototype.

let's say we have this object construction.
and we create an instance of the object Car.
Our object "mercedes" will inherit all properties and methods from the object "Car". which means, "mercedes" can access all the properties within the prototype of "Car".

which means if we add a new property or method, to our object construction "Car", "mercedes" the object will have access to that property or method.

Now let's say you just created a bunch of instances of the object "Car", and you forgot, to create a method, you don't have to go and add that method to every instance one by one. all you have to do is to add that method to the prototye of the object "Car".

now if you run this line of code.
You'll get this result: The name is Mercedes and model is 2018.
Why? because "mercedes" has access to "Car's" prototype, which means "mercedes" has access "Car's" properties and methods.

For more details on JavaScript Prototypes, watch this tutorial.


Comments

You're welcome to share your ideas with us in comments.

Archive

Contact Form

Send