I’m reading about prototypes, but I can’t seem to understand what they do and what the point of having them is. Can anyone create a simple example (ex. collegeStudent object) that will help me understand the basic concepts of prototypes? Thanks!
Share
Let’s say you create a function called
person:Now let’s say you’ve created some new persons:
personcurrently has three properties,name,age, gender.Using prototype you can add a new property to the object AND to all previously instantiated objects.
person.prototype.hairColor = null;<– If you set this to “brown” all previously instantiated objects will have the value “brown”. Sofred.hairColorwould be brown.The great thing about this is that you can set all previously instantiated and future objects a default value without having to manually set the property on all of those objects.