The prototypes

Have you wondered, how come you have access to methods such as valueOf, toString etc… backed in your object out of the box ?

While this would be a cool magic trick, in JavaScript, every object, has a fallback object, called the prototype. A prototype is another object that is used as a fallback source property. When a property is looked up on an object [obj].property, the property is first looked upon the object, and if it does not find it , it goes upper a level, to the prototype object.

Why do we need the prototype

The main idea behind having objects, is to have our data and functionality bundled together in one place, so that they can be easy to manage and reason about.

Other than having a concise way of working with our data and functionality that applies to them in one place, the prototype ads an extra feature of avoiding repetition when creating instances or different object, by sharing methods across different instances of objects.

Classes

Classes are simply constructor functions with a prototype property that save methods shared across these different instances of that class.

The class keyword was introduces in the es6 to make working with objects and prototype easier. It automates a lot of things

Map objects,

Maps is just another data structure that stores key value pairs, just like objects, but you can have pretty much anything as your key , rather than strings only.

Compared to the object literal, the map object, provides methods to help you interact with your object. you can set values, get the size, check for a property existence etc..

Polymorphism

Polymorphism which simply means many forms, is the ability of creating methods or functions that can take different forms accordingly to how and where they are being called.

A good example with the toString method that returns the given value in a string form(in case it’s a primitive) and for objects, it tries to convert the given object to it’s string representation. This is a perfect example of a function that takes many forms accordingly to where it gets called.