__proto__ and prototype in JavaScript

Photo by Mohammad Rahmani on Unsplash

Shadowing

Shadowing refers to a behavior when the __proto__ object and current object itself have same key. In this case, the priority is given to the object’s key instead of the __proto__ one.

The Object Prototype

Consider following code snippet:

let obj ={}; console.log(obj)

Now, here comes prototype to the rescue. Using prototype property is a two step process.

  1. Instead of attaching function to __proto__ , attach it directly to function using prototype property.
  2. Use new keyword to create the object.

To conclude, a function’s prototype specifies the __proto__ of the objects created by calling that function with a new keyword.

And in short, If someone asks what is prototype?
Answer: Prototype is just a property, that every function, we create in JavaScript has that points to an object. Generally we use to share a piece of code across our application.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store