Prototypes in JS

code_thoughts
1 min readOct 7, 2014
  • JavaScript has no classes.
  • Prototypes are like links to another object.
  • Every object has a prototype chain that ends in null.

An analogy that comes to mind is matter. You can have a boulder. If we walk up its “prototype chain” we discover its mineral composition. Further up we have molecules, then elements, then its atomic structure, then its subatomic structure (quarks?bosons?uh…). You go on and on until you get to the unknown depths of physics… null.

Similarly, all JS objects have a prototype chain. Objects can inherit methods and properties from the objects in its prototype chain. For instance, arrays and strings have a “.length” method that belongs to some object in their prototype chain.

If you try to access a property (with dot or bracket notation), JS will look for the property in the current object and if it doesn’t find it, walks up its prototype chain till it finds it. If it doesn’t, I believe it would return undefined or some other kind of error.

At the beginning of all prototype chains is… null.

https://www.youtube.com/watch?v=Fi-LsoKvn80

--

--