Prototype

I was hoping to learn what the "prototype" keyword means in JavaScript during my last college class. No suck luck. I am taking another JavaScript class next semester. But I thought I would try to get Google to help me figure out what this complex topics refers to.

We should start with what JavaScript has. You got primitives and objects. Primitives are types such as boolean, string, or number. Another type is an object. Objects are key value pairs. Keys are the properties of an object. The property is called a method if the value is a function. These are the basics.

Here is a true statement: All objects have prototypes. The default prototype only has the constructor. The constructor assigns the prototype to an object instance at the time of instance creation. You can retrieve this prototype by calling getPrototypeOf() or by accessing __proto__.

When you add properties to an instance, only that instance gets the properties by default. However you can use the prototype keyword before the property. That causes the property to apply to all instances of the obect. To be more specific, any instances created by the object will get the new properties added when the properties are qualified by the prototype keyword. Wow. That was a mouthful.

Now the prototype is itself an object. Therefore the prototype has a prototype. Let's not get too bogged down by that though. You can use prototype on your own objects. However you can only change a prebuilt object with prototype if the object was created with the "new" keyword.

The properties added to all instances via the prototype can be methods. When a property of an instance is accessed, the chain of prototypes is walked to determine if the property exists. This gives you insight into the internal structure of a JavaScript object. All objects inherit behavior from the prototype of Object. That would be the end of the prototype chain.