[javascript] __proto__ VS. prototype in JavaScript

To explain let us create a function

 function a (name) {
  this.name = name;
 }

When JavaScript executes this code, it adds prototype property to a, prototype property is an object with two properties to it:

  1. constructor
  2. __proto__

So when we do

a.prototype it returns

     constructor: a  // function definition
    __proto__: Object

Now as you can see constructor is nothing but the function a itself and __proto__ points to the root level Object of JavaScript.

Let us see what happens when we use a function with new key word.

var b = new a ('JavaScript');

When JavaScript executes this code it does 4 things:

  1. It creates a new object, an empty object // {}
  2. It creates __proto__ on b and makes it point to a.prototype so b.__proto__ === a.prototype
  3. It executes a.prototype.constructor (which is definition of function a ) with the newly created object (created in step#1) as its context (this), hence the name property passed as 'JavaScript' (which is added to this) gets added to newly created object.
  4. It returns newly created object in (created in step#1) so var b gets assigned to newly created object.

Now if we add a.prototype.car = "BMW" and do b.car, the output "BMW" appears.

this is because when JavaScript executed this code it searched for car property on b, it did not find then JavaScript used b.__proto__ (which was made to point to 'a.prototype' in step#2) and finds car property so return "BMW".

Examples related to javascript

β€’ need to add a class to an element β€’ How to make a variable accessible outside a function? β€’ Hide Signs that Meteor.js was Used β€’ How to create a showdown.js markdown extension β€’ Please help me convert this script to a simple image slider β€’ Highlight Anchor Links when user manually scrolls? β€’ Summing radio input values β€’ How to execute an action before close metro app WinJS β€’ javascript, for loop defines a dynamic variable name β€’ Getting all files in directory with ajax

Examples related to prototype

β€’ What are the nuances of scope prototypal / prototypical inheritance in AngularJS? β€’ Extending an Object in Javascript β€’ __proto__ VS. prototype in JavaScript β€’ Parse JSON String into a Particular Object Prototype in JavaScript β€’ Understanding the difference between Object.create() and new SomeFunction() β€’ 'this' is undefined in JavaScript class methods β€’ JavaScript: What are .extend and .prototype used for? β€’ Calling method using JavaScript prototype β€’ Use of 'prototype' vs. 'this' in JavaScript?

Examples related to javascript-objects

β€’ Cannot read property 'style' of undefined -- Uncaught Type Error β€’ Is this a good way to clone an object in ES6? β€’ What’s the difference between β€œ{}” and β€œ[]” while declaring a JavaScript array? β€’ Comparing two arrays of objects, and exclude the elements who match values into new array in JS β€’ Converting JavaScript object with numeric keys into array β€’ From an array of objects, extract value of a property as array β€’ How to copy JavaScript object to new variable NOT by reference? β€’ Remove property for all objects in array β€’ Using curl POST with variables defined in bash script functions β€’ How to sum the values of a JavaScript object?

Examples related to prototypal-inheritance

β€’ What are the nuances of scope prototypal / prototypical inheritance in AngularJS? β€’ __proto__ VS. prototype in JavaScript