<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lalit Aditya</title>
    <description>The latest articles on DEV Community by Lalit Aditya (@lalitaditya).</description>
    <link>https://dev.to/lalitaditya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3737341%2F59d848fc-bdd1-4d7b-aed6-63c81616bcf2.jpeg</url>
      <title>DEV Community: Lalit Aditya</title>
      <link>https://dev.to/lalitaditya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lalitaditya"/>
    <language>en</language>
    <item>
      <title>Prototypical Inheritance in JavaScript</title>
      <dc:creator>Lalit Aditya</dc:creator>
      <pubDate>Tue, 31 Mar 2026 14:20:56 +0000</pubDate>
      <link>https://dev.to/lalitaditya/prototypical-inheritance-in-javascript-54ef</link>
      <guid>https://dev.to/lalitaditya/prototypical-inheritance-in-javascript-54ef</guid>
      <description>&lt;p&gt;&lt;strong&gt;Prototypical inheritance&lt;/strong&gt; is one of the core concepts in JavaScript.&lt;/p&gt;

&lt;p&gt;But at the same time it is considered as one of the tough topics to understand. Even the &lt;strong&gt;experienced JS developers&lt;/strong&gt; fall short in confidence while dealing with inheritance/prototypical inheritance in JS.&lt;/p&gt;

&lt;p&gt;But to be honest, prototypical inheritance doesn't have any room for &lt;strong&gt;ambiguity&lt;/strong&gt;, and it's pretty straightforward to understand. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In this post, I will deep dive into the core concepts of JS related to &lt;strong&gt;prototype&lt;/strong&gt;. What exactly is a &lt;strong&gt;prototype&lt;/strong&gt;? What is the role of the &lt;code&gt;new&lt;/code&gt; keyword in prototypical inheritance, and why is this inheritance called &lt;strong&gt;prototypical inheritance&lt;/strong&gt;? &lt;strong&gt;Internal working&lt;/strong&gt; and &lt;strong&gt;ES6 updates&lt;/strong&gt; regarding prototypical inheritance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;The Confusion&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;new&lt;/code&gt; keyword and its role in prototypical inheritance&lt;/li&gt;
&lt;li&gt;Objects in JS&lt;/li&gt;
&lt;li&gt;Inheritance chain (instance and static)&lt;/li&gt;
&lt;li&gt;ES6 updates&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The Confusion:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The biggest confusion that almost everyone faces while trying to understand the prototypical inheritance is the word &lt;code&gt;prototype&lt;/code&gt; itself.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;Because this is actually confusing, as per the ECMA docs, the word &lt;strong&gt;prototype&lt;/strong&gt; is used in two different places, and that too, to represent two different entities altogether&lt;/p&gt;

&lt;p&gt;&lt;br&gt;One is the &lt;strong&gt;[[prototype]]&lt;/strong&gt;, and the other is the property named &lt;code&gt;prototype&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now let's settle the dust.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;[[prototype]]&lt;/strong&gt; refers to the &lt;code&gt;__proto__&lt;/code&gt; property. Every object in JavaScript has a default, built-in property named &lt;code&gt;__proto__&lt;/code&gt;. And the same &lt;code&gt;__proto__&lt;/code&gt; property represents the &lt;strong&gt;[[prototype]]&lt;/strong&gt; mentioned in the docs. For the sake of simplicity, in this post, whenever I want to refer to this &lt;strong&gt;[[prototype]]&lt;/strong&gt;, I will always refer to it as &lt;code&gt;__proto__&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The property named &lt;code&gt;prototype&lt;/code&gt; - In JavaScript, every function created by default has a property named &lt;code&gt;prototype&lt;/code&gt; attached to its object. (Yes, a &lt;strong&gt;function&lt;/strong&gt; is also an &lt;strong&gt;object&lt;/strong&gt; in JavaScript, but a &lt;strong&gt;special&lt;/strong&gt; kind of object; we will discuss the &lt;strong&gt;specialty&lt;/strong&gt; of this object later in this post.)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;br&gt;Think of it like a function is an object, and it has a key named &lt;code&gt;prototype&lt;/code&gt; on it. We didn't add this key to the function object; it's present by default. And the value of this key is again an object.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;So a &lt;strong&gt;function object&lt;/strong&gt; in JS has &lt;strong&gt;2 properties&lt;/strong&gt; by default. &lt;br&gt;
 One is &lt;code&gt;__proto__&lt;/code&gt; because a function is also an object, and every object has &lt;code&gt;__proto__&lt;/code&gt; &lt;br&gt;
 And the other is &lt;code&gt;prototype&lt;/code&gt; because it's a function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In simple terms, only &lt;strong&gt;function objects&lt;/strong&gt; get access to a special default key named &lt;code&gt;prototype&lt;/code&gt; along with &lt;code&gt;__proto__&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This might sound confusing; let's take an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the JS engine sees this instruction, it actually creates an object in the background. And since this object is a function object, as discussed, it will have a default property added to it named &lt;code&gt;prototype&lt;/code&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// name of the function&lt;/span&gt;
  &lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// referring to 2 parameters the function add has&lt;/span&gt;
  &lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// this key is added by default as add is a function&lt;/span&gt;
     &lt;span class="c1"&gt;// .... (some default key-value pairs would be present, we will see them later)&lt;/span&gt;
     &lt;span class="nl"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="c1"&gt;// wait what ? what is this ? &lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nl"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="c1"&gt;// again wait what ? what is this ?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Wherever we see an object ({}), we see a &lt;code&gt;__proto__&lt;/code&gt; being present, because every object in JS has a &lt;code&gt;__proto__&lt;/code&gt; property&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, &lt;code&gt;add&lt;/code&gt; is just a reference to this object present in memory, and using &lt;code&gt;add&lt;/code&gt; we can access this object.&lt;/p&gt;

&lt;p&gt;For example, we can add custom key-value pairs to this object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myCustomKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myCustomFn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Add custom function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myCustomKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 10&lt;/span&gt;
&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;myCustomFn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Add custom function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you observed, there were a couple of places where &lt;code&gt;__proto__&lt;/code&gt; was used above in the function object, and we had no idea why at one place &lt;code&gt;Object.prototype&lt;/code&gt; is present and why at the other place &lt;code&gt;Function.prototype&lt;/code&gt; is present. And that's completely fine. We will clear up this confusion in the coming sections.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;To summarize, every object in JavaScript has a built-in property named &lt;code&gt;__proto__&lt;/code&gt; and since a function is also an object in JavaScript, even the function object also has a &lt;code&gt;__proto__&lt;/code&gt; property, but as discussed, since a function is a special object, apart from the built-in &lt;code&gt;__proto__&lt;/code&gt; property, it also has one more special property named &lt;code&gt;prototype&lt;/code&gt;, which is again an object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Mental Model&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If object, then has access to the &lt;code&gt;__proto__&lt;/code&gt; property.&lt;/li&gt;
&lt;li&gt;If the object is a function object, then it has access to the &lt;code&gt;__proto__&lt;/code&gt; and &lt;code&gt;prototype&lt;/code&gt; properties.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// available&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// available&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// available because add is a function&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// avaialbe, because prototype itself is an object, and every object has __proto__ property&lt;/span&gt;
&lt;span class="c1"&gt;// All these properties are present by deafult. Added by langugage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;So from now on in this post, when &lt;code&gt;__proto__&lt;/code&gt; is referred to, we are talking about the &lt;strong&gt;default property&lt;/strong&gt; present in &lt;strong&gt;every object&lt;/strong&gt;, and when &lt;code&gt;prototype&lt;/code&gt; is referred to, we are talking about the &lt;strong&gt;default property&lt;/strong&gt; present in a &lt;strong&gt;function object&lt;/strong&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  new keyword and its role in prototypical inheritance:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; keyword in JavaScript is used to create an object.&lt;br&gt;
Syntax: &lt;code&gt;new fn()&lt;/code&gt;&lt;br&gt;
&lt;br&gt;Here, &lt;code&gt;fn&lt;/code&gt; refers to a &lt;strong&gt;function&lt;/strong&gt; or a &lt;strong&gt;constructor function&lt;/strong&gt;, and the &lt;code&gt;new&lt;/code&gt; keyword must be used with a function; otherwise, it results in an error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But most importantly in this process of &lt;strong&gt;object creation&lt;/strong&gt;, it also sets the &lt;code&gt;__proto__&lt;/code&gt; property of the newly created object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We still don't know what exactly this &lt;code&gt;__proto__&lt;/code&gt; property is. But we learned the fact that it exists by default on every object. We will see what exactly this &lt;code&gt;__proto__&lt;/code&gt; is in the next section&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Internal working of &lt;code&gt;new&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
When &lt;code&gt;new fn()&lt;/code&gt; is executed, there are some predefined sets of activities that it performs. They are:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Create a brand new object&lt;/li&gt;
&lt;li&gt;Bind &lt;code&gt;this&lt;/code&gt; to the newly created object.&lt;/li&gt;
&lt;li&gt;Set the &lt;code&gt;__proto__&lt;/code&gt; of the newly created object to &lt;code&gt;fn's prototype&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Run the function and return the newly created object (implicit return) if no explicit return is mentioned in &lt;code&gt;fn&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's understand it by an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;u&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's try to simulate the work done by the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;So think of something like below. (Not exactly, but more of a pseudocode)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// const obj = {}; // 1. creation of a new object&lt;/span&gt;
    &lt;span class="c1"&gt;// this = obj     // 2. binding this to new object created&lt;/span&gt;
    &lt;span class="c1"&gt;// this.__proto__ = User.prototype // 3. sets the newly created object __proto__ to User.prototype&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// return this;  // 4. implict return added, new object returned&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;So, as we can see, the &lt;code&gt;new&lt;/code&gt; keyword plays an important role by not only creating a new object but also setting the created object's &lt;code&gt;__proto__&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;We will see the significance of this step in the next section and how this step lays out the &lt;strong&gt;foundation for prototypical inheritance&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now I assume there shouldn't be any doubt/confusion in &lt;code&gt;User.prototype&lt;/code&gt;. Since User is a function object, it will have access to a property named &lt;code&gt;prototype&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// {} &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Objects in JS:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;In the previous 2 sections, we learned that every object in JavaScript has access to an inbuilt property named &lt;code&gt;__proto__&lt;/code&gt; and if the object is a function object, then along with &lt;code&gt;__proto__&lt;/code&gt; it will also have access to &lt;code&gt;prototype&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;But what exactly are these predefined properties? What values do these properties hold? What's their significance?&lt;/p&gt;

&lt;p&gt;&lt;br&gt;Let's try to answer that.&lt;br&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In simple terms, &lt;code&gt;__proto__&lt;/code&gt; defines where &lt;strong&gt;JavaScript should continue looking when a property is not found on an object itself.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;When you try to access something on an object and it doesn’t exist there, JavaScript follows the &lt;code&gt;__proto__&lt;/code&gt; link to another object and continues the search.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;strong&gt;And that's it; that's the whole purpose of &lt;code&gt;__proto__&lt;/code&gt; in JS&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;But since &lt;code&gt;__proto__&lt;/code&gt; is a default, built-in property present in every object, it also has a default value, and it is &lt;code&gt;Object.prototype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;br&gt; But now what is this &lt;code&gt;Object.prototype&lt;/code&gt; ?&lt;br&gt;
Before answering it, let's look at a few examples&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Exercise&lt;/strong&gt;: Try to perform all 4 activities done by the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;-    Were you able to find any common observation? I think from the above code snippet, the easiest instruction to understand would be &lt;code&gt;const u = new User('Peter', 25)&lt;/code&gt;, as we clearly discussed this case while we were trying to understand the &lt;code&gt;new&lt;/code&gt; keyword&lt;/p&gt;

&lt;p&gt;-    Here, "User" is a function or, more specifically, a &lt;strong&gt;constructor function&lt;/strong&gt; since it is invoked via the &lt;code&gt;new&lt;/code&gt; keyword. Hey, but wait, we also learned that the syntax of the &lt;code&gt;new&lt;/code&gt; keyword is &lt;code&gt;new fn()&lt;/code&gt;, where fn is a function&lt;/p&gt;

&lt;p&gt;-    Then how are we able to do &lt;code&gt;new Number(10)&lt;/code&gt;, &lt;code&gt;new String('Peter')&lt;/code&gt;, &lt;code&gt;new Promise((resolve) =&amp;gt; resolve({data: 'success'}))&lt;/code&gt;? Why does the &lt;code&gt;new&lt;/code&gt; keyword work here?&lt;/p&gt;

&lt;p&gt;-    The idea is actually simple: it works because, under the hood, all of these are &lt;strong&gt;constructor functions&lt;/strong&gt;. In fact, every built-in type you use—&lt;code&gt;Number&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Promise&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt; and others—is essentially a &lt;strong&gt;predefined constructor function&lt;/strong&gt; provided by the language itself. And hence the &lt;code&gt;new&lt;/code&gt; keyword works.&lt;/p&gt;

&lt;p&gt;We can check this by the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// function&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// function&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// function&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Now the &lt;strong&gt;two most important built-in types&lt;/strong&gt; provided by the language are &lt;code&gt;Object&lt;/code&gt; and &lt;code&gt;Function&lt;/code&gt;. And these are constructor functions.&lt;/p&gt;

&lt;p&gt;&lt;br&gt; From previous sections, we learned that every function object will have a built-in property named &lt;code&gt;prototype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;br&gt; Hence we can do the below&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;// function&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// function&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// {}  &lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// {}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;    &lt;code&gt;Object.prototype&lt;/code&gt; and &lt;code&gt;Function.prototype&lt;/code&gt; aren't actually &lt;strong&gt;empty objects&lt;/strong&gt;; why we see empty objects is because all the properties of &lt;code&gt;Object.prototype&lt;/code&gt; and &lt;code&gt;Function.prototype&lt;/code&gt; are &lt;strong&gt;non-enumerable&lt;/strong&gt;, hence the &lt;code&gt;console.log&lt;/code&gt; doesn't show them&lt;/p&gt;

&lt;p&gt;    But we can check the properties of these objects by the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getOwnPropertyDescriptors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getOwnPropertyDescriptors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;    The above instructions give all the properties present in the Object and Function predefined functions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Now let's get back to the default value of &lt;code&gt;__proto__&lt;/code&gt;,. As discussed, the default value is &lt;code&gt;Object.prototype&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt; Meaning, if the property being searched for isn't present in the object, continue the search in &lt;code&gt;Object.prototype&lt;/code&gt;, because that's what is present in &lt;code&gt;__proto__&lt;/code&gt;. For ex:&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// '[object Object]'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;     But wait, in the above code snippet, I never defined any property named &lt;code&gt;toString&lt;/code&gt; on my obj, but still somehow we can access the &lt;code&gt;toString&lt;/code&gt; property&lt;/p&gt;

&lt;p&gt;     And what we just discovered is the &lt;strong&gt;prototypical inheritance&lt;/strong&gt; in JS&lt;/p&gt;

&lt;p&gt;     Let's dissect the flow&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When we tried to access &lt;code&gt;toString&lt;/code&gt; on &lt;code&gt;obj&lt;/code&gt;,  the JS engine first searched &lt;code&gt;toString&lt;/code&gt; on obj. Since &lt;code&gt;toString&lt;/code&gt; wasn't present in &lt;code&gt;obj&lt;/code&gt;, the JS engine checked the &lt;code&gt;__proto__&lt;/code&gt; of &lt;code&gt;obj&lt;/code&gt;, it had the value of &lt;code&gt;Objet.prototype&lt;/code&gt;, so now the JS engine continued the search in &lt;code&gt;Obect.prototype&lt;/code&gt;. In &lt;code&gt;Object.prototype&lt;/code&gt;, we find the key &lt;code&gt;toString&lt;/code&gt; whose value is a function, and invoking it gave us the result &lt;code&gt;'[object Object]'&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;What if &lt;code&gt;toString&lt;/code&gt; didn’t exist on &lt;code&gt;Object.prototype&lt;/code&gt;? The idea is straightforward: &lt;strong&gt;JavaScript would continue the lookup&lt;/strong&gt; using the same mechanism—by following the &lt;code&gt;__proto__&lt;/code&gt; chain. It would check the &lt;code&gt;__proto__&lt;/code&gt; of &lt;code&gt;Object.prototype&lt;/code&gt; and keep searching upward.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;__proto__&lt;/code&gt; property would be present in &lt;code&gt;Object.prototype&lt;/code&gt; as Object.prototype is an object, and every object in JS will have a &lt;code&gt;__proto__&lt;/code&gt; property&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The catch, however, is that &lt;code&gt;__proto__&lt;/code&gt; on &lt;code&gt;Object.prototype&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt;, which signals the end of the prototype chain—there’s nowhere further to look, so the search stops there.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;So from the above discussion, we understand a couple of things.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;1. The default value of &lt;code&gt;__proto__&lt;/code&gt; in any object is &lt;code&gt;Object.prototype&lt;/code&gt;.&lt;br&gt;
&lt;br&gt;2. The lookup stops if the property isn’t found on &lt;code&gt;Object.prototype&lt;/code&gt;, because its &lt;code&gt;__proto__&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt;—signaling the end of the prototype chain.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let's take one more example to understand:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Parker&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Parker&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-    The above example should clear up how the lookup chain works. Let's try to simulate the JS engine.&lt;/p&gt;

&lt;p&gt;-    obj is an object, and hence it will have a default built-in property named &lt;code&gt;__proto__&lt;/code&gt; with value &lt;code&gt;Object.prototype&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;-    Upon accessing &lt;code&gt;obj.name&lt;/code&gt;, search a property named &lt;code&gt;name&lt;/code&gt; on obj. If found, return the value; else, continue the search by checking the value of the &lt;code&gt;__proto__&lt;/code&gt; property of obj.&lt;/p&gt;

&lt;p&gt;-    Since the &lt;code&gt;name&lt;/code&gt; property was not found on obj, continue the search in &lt;code&gt;Object.prototype&lt;/code&gt; (because that's what is present in &lt;code&gt;obj.__proto__&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;-    Check for the &lt;code&gt;name&lt;/code&gt; property on &lt;code&gt;Object.prototype&lt;/code&gt;, if not found, continue the search by checking the value of the &lt;code&gt;__proto__&lt;/code&gt; property inside &lt;code&gt;Object.prototype&lt;/code&gt;. The value found is &lt;code&gt;null&lt;/code&gt;, meaning stop the lookup chain and return &lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;-    But after the first &lt;code&gt;console.log&lt;/code&gt;, we are doing something interesting; we are actually adding a property in &lt;code&gt;Object.prototype&lt;/code&gt;. So the &lt;code&gt;Object.property&lt;/code&gt; should look something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwy8myr6m3aep32kicw9o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwy8myr6m3aep32kicw9o.png" alt="Object.prototype" width="800" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-    Hence in the 2nd time lookup, we actually get a value of &lt;code&gt;Parker&lt;/code&gt; as this time it is found in &lt;code&gt;Object.prototype&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Now let's discuss the most &lt;strong&gt;interesting&lt;/strong&gt; part. Remember, in the earlier sections, we said that &lt;strong&gt;functions&lt;/strong&gt; are also objects in JS, but they are a &lt;strong&gt;special kind of object&lt;/strong&gt;. But why is that?&lt;/p&gt;

&lt;p&gt;&lt;br&gt; We learned a rule that every object in JS will have a &lt;code&gt;__proto__&lt;/code&gt; property, and its value will be &lt;code&gt;Object.prototype&lt;/code&gt;. But this isn't true in the case of &lt;strong&gt;function objects&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;For every function object (either custom or predefined), &lt;code&gt;__proto__&lt;/code&gt; is equal to &lt;code&gt;Function.prototype&lt;/code&gt;. This can be easily checked as follows.&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="c1"&gt;// the last 2 cases are also true, because Function, Object are also functions in JS. This can be checked using typeof operator&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Now this should also explain why &lt;strong&gt;call, apply, and bind&lt;/strong&gt; functions can be invoked on every function in JS.&lt;/p&gt;

&lt;p&gt;&lt;br&gt; The reason is they exist in &lt;strong&gt;Function.prototype&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getOwnPropertyDescriptors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// call, apply, bind are properites inside Function.prototype&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Let's understand by taking one more example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; says Hi`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-    Let's again try to simulate the JS engine when &lt;code&gt;wish.call(obj1)&lt;/code&gt; is invoked.&lt;/p&gt;

&lt;p&gt;-    We know the &lt;strong&gt;function wish&lt;/strong&gt; creates an object, and &lt;code&gt;wish&lt;/code&gt; is a reference to the object.&lt;/p&gt;

&lt;p&gt;-    When a property named &lt;code&gt;call&lt;/code&gt; is accessed on &lt;code&gt;wish&lt;/code&gt;, first the property &lt;code&gt;call&lt;/code&gt; is searched in the object referred to by &lt;code&gt;wish&lt;/code&gt;. It isn't present, so continue the search by checking the &lt;code&gt;__proto__&lt;/code&gt; of the object referred to by &lt;code&gt;wish&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;-    Since &lt;code&gt;wish&lt;/code&gt; is a function object, its &lt;code&gt;__proto__&lt;/code&gt; has the value &lt;code&gt;Function.prototype&lt;/code&gt;, so the lookup is continued in &lt;code&gt;Function.prototype&lt;/code&gt;&lt;br&gt;
-    In &lt;code&gt;Function.prototype&lt;/code&gt;, we find the property &lt;code&gt;call&lt;/code&gt; which is a function, and hence the &lt;code&gt;wish.call&lt;/code&gt; is executed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The final mental model:&lt;/p&gt;

&lt;p&gt;&lt;br&gt;1. If an object then has access to the &lt;code&gt;__proto__&lt;/code&gt; property whose value is &lt;code&gt;Ojbect.prototype&lt;/code&gt;&lt;br&gt;
&lt;br&gt;2. If the object is a &lt;strong&gt;function object&lt;/strong&gt;, then it has access to the &lt;code&gt;__proto__&lt;/code&gt; property and &lt;code&gt;prototype&lt;/code&gt; property, and the value of &lt;code&gt;__proto__&lt;/code&gt; is &lt;code&gt;Function.prototype&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff0f7pqqeirp6216p89zo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff0f7pqqeirp6216p89zo.png" alt="The chain" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Observations&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Since &lt;code&gt;wish&lt;/code&gt;,&lt;code&gt;Function&lt;/code&gt;, and &lt;code&gt;Object&lt;/code&gt; are functions (&lt;code&gt;wish&lt;/code&gt; being custom-defined and &lt;code&gt;Function&lt;/code&gt;, &lt;code&gt;Object&lt;/code&gt; being predefined), they have access to both &lt;code&gt;__proto__&lt;/code&gt; and &lt;code&gt;prototype&lt;/code&gt; properties, whereas &lt;code&gt;obj&lt;/code&gt; is just a normal object and hence has access to only the &lt;code&gt;__proto__&lt;/code&gt; property.&lt;/li&gt;
&lt;li&gt;Every function object's &lt;code&gt;__proto__&lt;/code&gt; is &lt;code&gt;Function.prototype&lt;/code&gt; and normal object's (obj's) &lt;code&gt;__proto__&lt;/code&gt; is &lt;code&gt;Object.prototype&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;prototype&lt;/code&gt; property of a function object always has a predefined property named &lt;code&gt;constructor&lt;/code&gt; whose value is the reference to the function itself (check the above diagram)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wish.prototype&lt;/code&gt;, &lt;code&gt;Function.prototype&lt;/code&gt;, and &lt;code&gt;Object.prototype&lt;/code&gt; are normal objects and not function objects, and hence they have the &lt;code&gt;__proto__&lt;/code&gt; value as &lt;code&gt;Object.prototype&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Object.prototype.__proto__&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt;—this is defined by the language itself. It marks the &lt;strong&gt;end of the prototype chain&lt;/strong&gt;, which is why &lt;code&gt;Object.prototype&lt;/code&gt; is considered the &lt;strong&gt;topmost level&lt;/strong&gt;, and property lookup stops here.&lt;/li&gt;
&lt;li&gt;This process—where a property is first searched on the object itself, and if not found, the lookup continues along its &lt;strong&gt;[[prototype]]&lt;/strong&gt; (&lt;code&gt;__proto__&lt;/code&gt; property) chain—is precisely why it’s called &lt;strong&gt;Prototypical inheritance&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;One final example:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;-    Let's become the JS engine again and try to look up the property &lt;code&gt;x&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;-    On the Function object, there is no property named &lt;code&gt;x&lt;/code&gt;, so check the &lt;code&gt;__proto__&lt;/code&gt; property of Function and continue the search there. The &lt;code&gt;__proto__&lt;/code&gt; of &lt;code&gt;Function&lt;/code&gt; is &lt;code&gt;Function.prototype&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;-    Now let's go to &lt;code&gt;Function.prototype&lt;/code&gt;. In &lt;code&gt;Function.prototype&lt;/code&gt; also there is no property named &lt;code&gt;x&lt;/code&gt;. Check the &lt;code&gt;__proto__&lt;/code&gt; in &lt;code&gt;Function.prototype&lt;/code&gt; and go there. The &lt;code&gt;__proto__&lt;/code&gt; of &lt;code&gt;Function.prototype&lt;/code&gt; is &lt;code&gt;Object.prototype&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;-    Now check the &lt;code&gt;Object.prototype&lt;/code&gt;. There is no property named &lt;code&gt;x&lt;/code&gt; in &lt;code&gt;Object.prototype&lt;/code&gt;, so check the &lt;code&gt;__proto__&lt;/code&gt; of &lt;code&gt;Object.prototype&lt;/code&gt;. It's &lt;code&gt;null&lt;/code&gt;. So stop the lookup and return &lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(Check the above diagram for any confusions.)&lt;/p&gt;
&lt;h2&gt;
  
  
  Inheritance chain (instance and static):
&lt;/h2&gt;

&lt;p&gt;The previous section could be heavy, but once that is cleared, there is not much left to understand in &lt;strong&gt;Prototypical inheritance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this section, let's see how to implement inheritance in JavaScript (via &lt;strong&gt;[[prototype]]&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;new&lt;/code&gt; keyword section, we saw that &lt;code&gt;new&lt;/code&gt; keywords set the newly created object's &lt;strong&gt;[[prototype]]&lt;/strong&gt; (&lt;code&gt;__proto__&lt;/code&gt; property) to the constructor function's &lt;code&gt;prototype&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;RateLimiter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;limit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;user1RateLimiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RateLimiter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;user2RateLimiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RateLimiter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;user1RateLimiter&lt;/code&gt; and &lt;code&gt;user2RateLimiter&lt;/code&gt; are two objects from the &lt;code&gt;RateLimiter&lt;/code&gt; constructor function&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exercise&lt;/strong&gt;: Try to run the 4 activities done by the &lt;code&gt;new&lt;/code&gt; keyword&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the memory, 2 objects are created, and each object will have its own copy of &lt;code&gt;limit&lt;/code&gt; and &lt;code&gt;requests&lt;/code&gt;, because the &lt;code&gt;new&lt;/code&gt; keyword creates a new object and returns it (implicit return). This can be checked by following
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;user1RateLimiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;user2RateLimiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user1RateLimiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 6&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user2RateLimiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 11&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Now what if I need to add a method named &lt;code&gt;isRequestAllowed&lt;/code&gt; to check whether the current request should be allowed or not? Basically, we need to check whether the current request number is less than the defined limit or not.&lt;/p&gt;

&lt;p&gt;&lt;br&gt; For this, we need to define a method, but where should we define that method? The only condition is this method should be available to all the &lt;strong&gt;instances created&lt;/strong&gt; out of the &lt;strong&gt;constructor function&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;What if we define the method &lt;code&gt;isRequestAllowed&lt;/code&gt; in the &lt;code&gt;RateLimiter&lt;/code&gt; function object itself? Will this suffice for our requirement?&lt;/p&gt;

&lt;p&gt;&lt;br&gt; Check the following:&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F19e9d2a2pml890hq6xpr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F19e9d2a2pml890hq6xpr.png" alt="methods present on function" width="800" height="536"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Clearly this doesn't work, because when we try to access the method &lt;code&gt;isRequestAllowed&lt;/code&gt; on the instance &lt;code&gt;user1RateLimiter&lt;/code&gt; (run the algorithm we learned in the previous section), we get &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;br&gt; Because we first search the property &lt;code&gt;isRequestAllowed&lt;/code&gt; on the &lt;code&gt;user1RateLimiter&lt;/code&gt; object, since it's not found, we check the &lt;code&gt;__proto__&lt;/code&gt; which has the value &lt;code&gt;RateLimiter.prototype&lt;/code&gt; (the &lt;code&gt;new&lt;/code&gt; keyword sets this), and even in &lt;code&gt;RateLimiter.prototype&lt;/code&gt; there is no property named &lt;code&gt;isRequestAllowed&lt;/code&gt;, propagating the search to &lt;code&gt;RateLimiter.protoytpe.__proto&lt;/code&gt; which points to &lt;code&gt;Object.prototype&lt;/code&gt;. Since &lt;code&gt;Object.prototype&lt;/code&gt; also doesn't have any property named &lt;code&gt;isRequestAllowed&lt;/code&gt;, the search propagates to &lt;code&gt;Object.prototype.__proto__&lt;/code&gt; whose value is &lt;code&gt;null&lt;/code&gt;, hence leading to &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So defining the method on the &lt;strong&gt;function object&lt;/strong&gt; itself didn't help us. The correct place to put these methods would be &lt;code&gt;RateLimiter.prototype&lt;/code&gt;, the reason being &lt;strong&gt;all the objects created from the constructor function &lt;code&gt;RateLimter&lt;/code&gt; will have the same &lt;code&gt;__proto__&lt;/code&gt; i.e, &lt;code&gt;RateLimiter.prototype&lt;/code&gt;&lt;/strong&gt;, and hence defining the method in &lt;code&gt;RateLimiter.prototype&lt;/code&gt; will make sure that each instance created from &lt;code&gt;RateLimiter&lt;/code&gt; can access the method.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsdxon5jp5umyopplnfea.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsdxon5jp5umyopplnfea.png" alt="methods present in the prototype" width="800" height="536"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user1RateLimiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;user1RateLimiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// true, because both point to RateLimiter.prototype because of new keyword&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user1RateLimiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isRequestAllowed&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;user2RateLimiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isRequestAllowed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true, because it's the same method both objects reach in chain&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now let's extend the same concept to achieve &lt;strong&gt;Parent-Child relationship&lt;/strong&gt; in JavaScript. Consider the following example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Notification&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="nx"&gt;Notification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;formatMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`[Notification]: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;EmailNotification&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="nx"&gt;EmailNotification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Email sent with message: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;emailNotification&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EmailNotification&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emailNotification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Email sent with message: Hello&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emailNotification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;formatMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hey there!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-    It would be clear why the 2nd &lt;code&gt;console.log&lt;/code&gt; gives &lt;code&gt;undefined&lt;/code&gt;, simply because the &lt;code&gt;formatMessage&lt;/code&gt; property was never found in the prototype chain (&lt;code&gt;EmailNotification.prototype&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;-    But we want the &lt;code&gt;formatMessage&lt;/code&gt; method defined on the Notification function prototype to be available on the &lt;code&gt;emailNotification&lt;/code&gt; instance&lt;/p&gt;

&lt;p&gt;-    Meaning we want to establish a relationship between the EmailNotification function and the Notification function, so when a property isn't found on &lt;code&gt;Notification.prototype&lt;/code&gt;, the current next searched place is &lt;code&gt;Object.prototype&lt;/code&gt;, because &lt;code&gt;Notifcation.prototype.__proto__&lt;/code&gt; is &lt;code&gt;Object.prototype&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;-    &lt;strong&gt;Current Relationship&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcxcsdgcrmfvpuzpi8gm5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcxcsdgcrmfvpuzpi8gm5.png" alt="No Parent-Child relationship" width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-    To reach the target, we just need to do a minor tweak to &lt;code&gt;EmailNotification.prototype&lt;/code&gt;. We want the chain to look like  &lt;code&gt;emailNotification -&amp;gt; EmailNotification.prototype -&amp;gt; Notification.prototype -&amp;gt; Object.prototype -&amp;gt; null&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;-    The magic line that will allow us to do this is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;EmailNotification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Notification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;By default, every object (except function objects) has its &lt;code&gt;__proto__&lt;/code&gt; pointing to &lt;code&gt;Object.prototype&lt;/code&gt;. However, if you want to create an object with a &lt;strong&gt;custom prototype&lt;/strong&gt;, &lt;code&gt;Object.create()&lt;/code&gt; allows you to do exactly that by explicitly setting the object’s &lt;code&gt;__proto__&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;-     So &lt;code&gt;Object.create(Notification.prototype)&lt;/code&gt; will create a new object whose &lt;code&gt;__proto__&lt;/code&gt; would be equal to &lt;code&gt;Notification.prototype&lt;/code&gt;. And this new object's reference is assigned to &lt;code&gt;EmailNotification.prototype&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzhd58tfgdy8601uebfgm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzhd58tfgdy8601uebfgm.png" alt="Parent-child relationship" width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-    The only downside is, as discussed in the previous section, the &lt;code&gt;prototype&lt;/code&gt; object present in the function will always have a default key named &lt;code&gt;constructor&lt;/code&gt; in it. But due to &lt;code&gt;Object.create(Notification.prototype)&lt;/code&gt;, we created an entirely new object, hence lost the constructor key, so as a good practice, let's add it back&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;EmailNotification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Notification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;EmailNotification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;EmailNotification&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-    And that's it; the above 2 lines will help us create the parent-child relationship (inheritance) in JavaScript&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The inheritance we observed above can be described as &lt;strong&gt;instance-based inheritance&lt;/strong&gt;, since the lookup chain is followed through the instance’s prototype (&lt;code&gt;__proto__&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now let's understand &lt;strong&gt;static inheritance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;Strictly speaking, JavaScript didn’t have an explicit concept of &lt;strong&gt;static inheritance&lt;/strong&gt; before &lt;strong&gt;ES6&lt;/strong&gt;. With the introduction of classes in &lt;strong&gt;ES6&lt;/strong&gt;, the &lt;code&gt;static&lt;/code&gt; keyword formalized this idea, enabling inheritance of static methods and properties across classes.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;The idea is simple: &lt;strong&gt;instance inheritance&lt;/strong&gt; works by looking up the &lt;strong&gt;[[Prototype]]&lt;/strong&gt; (&lt;code&gt;__proto__&lt;/code&gt;) chain starting from the instance created by the constructor function.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;In contrast, &lt;strong&gt;static inheritance&lt;/strong&gt; works by following the &lt;strong&gt;[[Prototype]]&lt;/strong&gt; (&lt;code&gt;__proto__&lt;/code&gt;) chain on the function itself (function object)—rather than on its instances.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let's take an example to understand:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;identify&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am an animal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-    If observed closely, we aren't defining the &lt;code&gt;identify&lt;/code&gt; method in the &lt;code&gt;Animal.prototype&lt;/code&gt; object; rather, we are defining this method on the &lt;code&gt;Animal&lt;/code&gt; function object itself, and these are what are considered &lt;code&gt;static&lt;/code&gt; methods in &lt;strong&gt;ES6&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;-    Now let's define the child class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;identify&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;identify&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// cannot invoke function on undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-    I hope at this point, we are clear why &lt;code&gt;Cat.idenify&lt;/code&gt; returns &lt;code&gt;undefined&lt;/code&gt;. Because the property &lt;code&gt;identify&lt;/code&gt; was not found in the function Cat &lt;strong&gt;[[prototype]]&lt;/strong&gt; chain (&lt;code&gt;__proto__&lt;/code&gt; chain). &lt;strong&gt;Search order&lt;/strong&gt;: &lt;code&gt;Cat -&amp;gt; Cat.__proto__ = Function.prototype -&amp;gt; Function.prototype.__proto__ = Object.prototype -&amp;gt; null&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fded8bvm3xll1kt4a8478.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fded8bvm3xll1kt4a8478.png" alt="No static relationship" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-    Now what we want is property &lt;code&gt;identify&lt;/code&gt; to be available to the function &lt;code&gt;Cat&lt;/code&gt;, meaning we have to tinker with the &lt;code&gt;__proto__&lt;/code&gt; chain such that, once not found in &lt;code&gt;Cat&lt;/code&gt;, the next search should happen in &lt;code&gt;Animal&lt;/code&gt; (and not in &lt;code&gt;Function.prototype&lt;/code&gt;).&lt;br&gt;
-    The magic line that will allow us to achieve this is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Animal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;identify&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// [Function]&lt;/span&gt;
&lt;span class="nx"&gt;conosole&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;identify&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// I am an animal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-    By doing the above, we are manipulating the &lt;code&gt;__proto__&lt;/code&gt; chain of &lt;code&gt;Cat&lt;/code&gt; to search next in the &lt;code&gt;Animal&lt;/code&gt; function object&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn1t8j28zsjnknrvs6r7b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn1t8j28zsjnknrvs6r7b.png" alt="Static relationship" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-    And what we witnessed above is called &lt;strong&gt;static inheritance&lt;/strong&gt; in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  ES6 Updates:
&lt;/h2&gt;

&lt;p&gt;Awesome that you have reached until here; what we learned in the previous sections officially completes the &lt;strong&gt;prototypical inheritance&lt;/strong&gt; in JavaScript.&lt;/p&gt;

&lt;p&gt;But &lt;strong&gt;ES6&lt;/strong&gt; released a set of new features that allows us to establish inheritance in an easier way.&lt;/p&gt;

&lt;p&gt;New keywords introduced in &lt;strong&gt;ES6&lt;/strong&gt; regarding prototypical inheritance: &lt;code&gt;class&lt;/code&gt;, &lt;code&gt;static&lt;/code&gt;, &lt;code&gt;extends&lt;/code&gt;, and &lt;code&gt;super&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. &lt;u&gt;The &lt;code&gt;class&lt;/code&gt; keyword&lt;/u&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Strictly speaking, the &lt;code&gt;class&lt;/code&gt; keyword is just &lt;strong&gt;syntactic sugar&lt;/strong&gt; for constructor functions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This was mainly for the developers who came from other ecosystems like Java, C#, etc. But under the hood it's the same &lt;strong&gt;constructor function&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; says Hi`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;isValidAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Peter&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// 25&lt;/span&gt;
&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;          &lt;span class="c1"&gt;// Peter says Hi&lt;/span&gt;
&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isValidAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;    Under the hood, the above class becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; says Hi`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isValidAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-    i) The code written inside a &lt;strong&gt;class constructor&lt;/strong&gt; essentially becomes the body of the underlying &lt;strong&gt;constructor function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;-    ii) The &lt;strong&gt;instance method&lt;/strong&gt; defined in the class (&lt;code&gt;wish&lt;/code&gt;) is added to the &lt;code&gt;User.prototype&lt;/code&gt; (so that instances can have access to this method)&lt;/p&gt;

&lt;p&gt;-    iii) The &lt;strong&gt;static method&lt;/strong&gt; defined in the class (defined via the &lt;code&gt;static&lt;/code&gt; keyword) is added directly as a property on the constructor function (&lt;code&gt;User&lt;/code&gt; function object).&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;new&lt;/code&gt; keyword works the same even for classes. The same 4 activities will be performed by the &lt;code&gt;new&lt;/code&gt; keyword in the background.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Classes in JavaScript, by default, will run in &lt;strong&gt;strict mode&lt;/strong&gt;. And can be invoked only via the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. &lt;u&gt;The &lt;code&gt;extends&lt;/code&gt; keyword&lt;/u&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The extends keyword is used to establish the parent-child relationship between classes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;generic animal sound&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Base class for all animals&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;meow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Meow!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;identify&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cat class&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;meow&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;       &lt;span class="c1"&gt;// Meow!&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;identify&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;   &lt;span class="c1"&gt;// Cat class&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;      &lt;span class="c1"&gt;// generic animal sound&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;   &lt;span class="c1"&gt;// Base class for all animals&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;As we can see, the &lt;strong&gt;child class instance&lt;/strong&gt; (&lt;code&gt;cat&lt;/code&gt;) can access properties and methods defined in the parent class (&lt;code&gt;Animal&lt;/code&gt;). This is made possible by the &lt;code&gt;extends&lt;/code&gt; keyword, which establishes the inheritance relationship between them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;    But what it does under the hood is what we do manually in the case of constructor functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// set instance inheritance&lt;/span&gt;
&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Cat&lt;/span&gt;

&lt;span class="c1"&gt;// set static inheritance&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;    We don't need to do the above manually; extends does it on behalf of us.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. &lt;u&gt;The &lt;code&gt;super&lt;/code&gt; keyword&lt;/u&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;super&lt;/code&gt; keyword allows us to call the &lt;strong&gt;parent class constructor function&lt;/strong&gt; to perform the initialization of the &lt;strong&gt;child object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;br&gt; Yes, you heard it right—calling &lt;code&gt;super()&lt;/code&gt; simply initializes the &lt;strong&gt;child class instance&lt;/strong&gt; using the &lt;strong&gt;parent’s constructor&lt;/strong&gt;; it does not create a &lt;strong&gt;separate parent class object&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This might sound surprising, especially to the devs coming from a Java background, where invoking &lt;code&gt;super()&lt;/code&gt; first creates the parent class object, but that isn't true in the case of JavaScript&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Because remember, all the new syntax that came in &lt;strong&gt;ES6&lt;/strong&gt; is just &lt;strong&gt;syntactic sugar&lt;/strong&gt; for the traditional construction functions. Invoking &lt;code&gt;super()&lt;/code&gt; simply means calling the &lt;strong&gt;parent constructor function&lt;/strong&gt; by passing the child's &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let's understand it by an example:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Animal constructor called&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// calls parent constructor on the SAME object&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cat constructor called&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Yapapa cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gold&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;    Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Animal constructor called
Cat constructor called
Yapapa cat
gold
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Under the hood, the above classes convert to the below constructor functions
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Animal constructor called&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// important: Calling Animal function with Cat's this&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cat constructor called&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// setting both instance and static inheritance between Animal and Cat&lt;/span&gt;
&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Yapapa cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gold&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now we know that because of the &lt;code&gt;new&lt;/code&gt; keyword (&lt;code&gt;new Cat('Yapapa cat','gold')&lt;/code&gt;), a new object would be created, and &lt;code&gt;this&lt;/code&gt; binds to the new object created, and the same object is passed in &lt;code&gt;Animal.call&lt;/code&gt;, and hence on the &lt;strong&gt;cat instance&lt;/strong&gt;, the &lt;code&gt;name&lt;/code&gt; property is added (by the &lt;code&gt;Animal&lt;/code&gt; constructor function).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A natural question that can arise is, why even initialize the properties in the &lt;strong&gt;parent class&lt;/strong&gt;, as the instance is always of the &lt;strong&gt;child class&lt;/strong&gt;, and why not directly perform the initialization of the instance in the &lt;strong&gt;child constructor function&lt;/strong&gt; itself?&lt;/p&gt;

&lt;p&gt;&lt;br&gt;The answer is simple: To not violate the &lt;strong&gt;DRY&lt;/strong&gt; principle, the main purpose of creating a &lt;strong&gt;parent class/parent constructor function&lt;/strong&gt; is to keep the common code together&lt;/p&gt;

&lt;p&gt;&lt;br&gt;A &lt;strong&gt;parent class/constructor function&lt;/strong&gt; can have many &lt;strong&gt;child classes/constructor functions&lt;/strong&gt;, so instead of initializing all the properties in the &lt;strong&gt;child class constructor&lt;/strong&gt; itself, a better practice is to perform the initialization of common properties in the &lt;strong&gt;parent class/constructor function&lt;/strong&gt; so that we don't have to repeat the initialization logic in each &lt;strong&gt;child class/constructor function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;Example:&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Vehicle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bike&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Vehicle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// common initialization&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Vehicle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;doors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// common initialization&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;doors&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;    As we could, we extracted out the common initialization logic to the &lt;strong&gt;parent class&lt;/strong&gt;, saving ourselves from repeating, and super in &lt;strong&gt;ES6&lt;/strong&gt; is a cleaner way to achieve this instead of &lt;code&gt;Car.call(this, brand, year)&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;This ends the post. I hope this post added value to your JS learning journey. If yes, please do leave a comment/reaction.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>prototype</category>
      <category>inheritance</category>
    </item>
    <item>
      <title>Understanding "this" in JS</title>
      <dc:creator>Lalit Aditya</dc:creator>
      <pubDate>Tue, 24 Mar 2026 17:18:43 +0000</pubDate>
      <link>https://dev.to/lalitaditya/understanding-this-in-js-b78</link>
      <guid>https://dev.to/lalitaditya/understanding-this-in-js-b78</guid>
      <description>&lt;p&gt;&lt;code&gt;this&lt;/code&gt; keyword in JS can be trickier to understand at first glance. The obvious reason could be the way the &lt;code&gt;this&lt;/code&gt; keyword behaves in JavaScript is different when compared to its peer languages like C++, Java, etc&lt;/p&gt;

&lt;p&gt;But to be honest, understanding the &lt;code&gt;this&lt;/code&gt; keyword in JavaScript is actually easier than it looks. &lt;/p&gt;

&lt;p&gt;There are some fixed sets of rules provided by &lt;strong&gt;ECMA&lt;/strong&gt;. So if we understand those rules, the &lt;code&gt;this&lt;/code&gt; keyword is &lt;strong&gt;done and dusted&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;By the end of this post, I assure every reader will gain complete knowledge of the &lt;code&gt;this&lt;/code&gt; keyword in JavaScript and will be able to use the &lt;code&gt;this&lt;/code&gt; keyword more &lt;strong&gt;confidently&lt;/strong&gt; in their projects.&lt;/p&gt;

&lt;p&gt;Now starting, before actually diving into the &lt;code&gt;this&lt;/code&gt; keyword and understanding its behavior, we have to know a few &lt;strong&gt;prerequisites&lt;/strong&gt;, and these prerequisites would majorly clear a lot of dust around the &lt;code&gt;this&lt;/code&gt; keyword in JS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;this&lt;/code&gt; keyword has a valid meaning only inside an &lt;strong&gt;execution context&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now what does that mean?&lt;/p&gt;

&lt;p&gt;JS has primarily &lt;strong&gt;2 execution contexts&lt;/strong&gt; defined in the language. (Actually, there are 4, but for the moment, we will skip the other 2 execution contexts (&lt;strong&gt;Eval, Module&lt;/strong&gt;)). They are&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Global Execution Context&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Functional Execution Context&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So there's the goal. Understanding &lt;code&gt;this&lt;/code&gt; in the above two mentioned execution contexts will help us understand &lt;strong&gt;95%&lt;/strong&gt; of the &lt;code&gt;this&lt;/code&gt; behavior in JS&lt;/p&gt;

&lt;h2&gt;
  
  
  1. &lt;u&gt;&lt;em&gt;this&lt;/em&gt; in Global Execution Context:&lt;/u&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Standard Rule&lt;/strong&gt;: &lt;code&gt;this&lt;/code&gt; in GEC (Global Execution Context) will always represent the global object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now what's this global object? The global object is provided by the Host&lt;/p&gt;

&lt;p&gt;If you remember, JS, or more specifically, &lt;strong&gt;ECMAScript&lt;/strong&gt;, is just a &lt;strong&gt;specification/contract&lt;/strong&gt; of the language, and the &lt;strong&gt;JS engines (V8, SpiderMonkey, etc.)&lt;/strong&gt; are the actual software that &lt;strong&gt;implements&lt;/strong&gt; this specification. &lt;/p&gt;

&lt;p&gt;These engines convert the &lt;strong&gt;JS code to machine-level code (binary)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And these JS engines are usually &lt;strong&gt;embedded&lt;/strong&gt; inside the &lt;strong&gt;host&lt;/strong&gt;.&lt;br&gt;
The &lt;strong&gt;host&lt;/strong&gt; can be a &lt;strong&gt;browser (Chrome, Safari, etc.)&lt;/strong&gt;, &lt;strong&gt;Node.js&lt;/strong&gt;, &lt;strong&gt;Bun&lt;/strong&gt;, etc&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So, based on the &lt;strong&gt;host&lt;/strong&gt;, the &lt;strong&gt;global object&lt;/strong&gt; might differ, and so might the value of the &lt;code&gt;this&lt;/code&gt; keyword. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the value of the &lt;code&gt;this&lt;/code&gt; keyword when checked on the &lt;strong&gt;browser console&lt;/strong&gt; vs. on &lt;strong&gt;Node REPL&lt;/strong&gt; would differ.&lt;/p&gt;

&lt;p&gt;But the main point is that the &lt;code&gt;this&lt;/code&gt; keyword in &lt;strong&gt;GEC&lt;/strong&gt; would always hold the value of the &lt;strong&gt;global object&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmof7jj7wcany8vblg01t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmof7jj7wcany8vblg01t.png" alt="this in browser" width="800" height="305"&gt;&lt;/a&gt;&lt;/p&gt;





&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5gc6y4a54jn6gxy1tfi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5gc6y4a54jn6gxy1tfi.png" alt="this in Node REPL" width="800" height="578"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, &lt;code&gt;this&lt;/code&gt; inside a &lt;strong&gt;browser&lt;/strong&gt; gives a &lt;strong&gt;window&lt;/strong&gt; object (as the &lt;strong&gt;window&lt;/strong&gt; object is the &lt;strong&gt;global object&lt;/strong&gt; inside a browser), but the &lt;code&gt;this&lt;/code&gt; inside &lt;strong&gt;Node REPL&lt;/strong&gt; gives a &lt;strong&gt;different object&lt;/strong&gt; with different properties.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
One of the very common misconceptions among many developers is that they assume the &lt;strong&gt;global object in Node.js&lt;/strong&gt; is &lt;strong&gt;{}&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because when we try to check the value of &lt;code&gt;this&lt;/code&gt; in a file and run that file via Node.js, we see &lt;strong&gt;{}&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvo142bztxwf726f26xr9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvo142bztxwf726f26xr9.png" alt="this in Node.js file" width="800" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But there's a &lt;strong&gt;catch&lt;/strong&gt;: a &lt;strong&gt;global object&lt;/strong&gt; cannot be empty, and this is clearly evident when we tried to see the value of &lt;code&gt;this&lt;/code&gt; in &lt;strong&gt;Node REPL.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Then why are we seeing an empty object {}?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer lies in how Node.js works internally. Any code that you write inside a file run by Node.js, &lt;strong&gt;Node.js will never run that file in the true global scope.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, why's that? Before &lt;strong&gt;ES6&lt;/strong&gt;, JS developers had only the &lt;code&gt;var&lt;/code&gt; keyword to declare variables, and variables declared with the &lt;code&gt;var&lt;/code&gt; keyword are stored in the &lt;strong&gt;global scope&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;So, if 2 files in a project declare the same variable using the &lt;code&gt;var&lt;/code&gt; keyword, it would pollute the global scope. Meaning the variable declared with &lt;code&gt;var&lt;/code&gt; would override its previous entry. &lt;br&gt;
&lt;strong&gt;Ex:&lt;/strong&gt; Let's say a project has 2 files named &lt;strong&gt;f1.js&lt;/strong&gt; and &lt;strong&gt;f2.js&lt;/strong&gt;. &lt;br&gt;
If &lt;strong&gt;f1.js&lt;/strong&gt; has declared a variable named with &lt;code&gt;hasPermission&lt;/code&gt; with the value &lt;code&gt;true&lt;/code&gt; and &lt;strong&gt;f2.js&lt;/strong&gt; has also declared a variable with the same name &lt;code&gt;hasPermission&lt;/code&gt; with the value &lt;code&gt;false&lt;/code&gt;, and assuming &lt;strong&gt;f1&lt;/strong&gt; runs first and &lt;strong&gt;f2&lt;/strong&gt; later, the final value of the &lt;code&gt;hasPermission&lt;/code&gt; variable would be &lt;code&gt;false&lt;/code&gt;, as running the &lt;strong&gt;f2.js&lt;/strong&gt; file has overridden the value of the &lt;code&gt;hasPermission&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;Now the results of the file &lt;strong&gt;f1.js&lt;/strong&gt; would be &lt;strong&gt;catastrophic&lt;/strong&gt;, as the whole logic of &lt;strong&gt;f1.js&lt;/strong&gt; would be &lt;strong&gt;affected&lt;/strong&gt; by this overriding, as all the variables declared by the &lt;code&gt;var&lt;/code&gt; keyword are stored in &lt;strong&gt;global scope&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;This was a big problem.&lt;/p&gt;

&lt;p&gt;Hence, Node.js provided a solution of its own. &lt;/p&gt;

&lt;p&gt;Node.js always &lt;strong&gt;wraps your file&lt;/strong&gt; inside a function, and that function would be &lt;strong&gt;internally invoked&lt;/strong&gt; so that every variable defined with the &lt;code&gt;var&lt;/code&gt; keyword will live only inside that file, as the function will create a &lt;strong&gt;separate execution context&lt;/strong&gt;, hence saving the global scope from being polluted.&lt;/p&gt;

&lt;p&gt;That also means that any code that is in a file when run by Node.js will be &lt;strong&gt;wrapped in a function&lt;/strong&gt;, and the code truly doesn't run in the &lt;strong&gt;global scope&lt;/strong&gt; of Node.js&lt;/p&gt;

&lt;p&gt;But still, the question is &lt;strong&gt;unanswered&lt;/strong&gt;. Why does this give an &lt;strong&gt;empty object&lt;/strong&gt;? This will be answered in the upcoming sections of the blog...&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;2. &lt;em&gt;this&lt;/em&gt; in Functional Execution Context:&lt;/u&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As we all know, functions in JS create a &lt;strong&gt;new execution context&lt;/strong&gt;; hence, the &lt;code&gt;this&lt;/code&gt; keyword will behave differently inside a function.&lt;br&gt;
&lt;strong&gt;But how different?&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Standard Rule:&lt;/strong&gt; The value of &lt;code&gt;this&lt;/code&gt; inside a function will depend on how the function is &lt;strong&gt;invoked&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that leads to a question: In how many ways can we invoke a function in JS?&lt;br&gt;
The answer is &lt;strong&gt;4&lt;/strong&gt;. They are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Function invocation&lt;/strong&gt; (the plain function call)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method invocation&lt;/strong&gt; (via an &lt;strong&gt;object reference&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explicit Binding&lt;/strong&gt; (using &lt;strong&gt;call/apply/bind&lt;/strong&gt; methods)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Constructor invocation&lt;/strong&gt; (using the &lt;strong&gt;new&lt;/strong&gt; keyword)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So depending on which type of invocation is used, &lt;code&gt;this&lt;/code&gt; will be populated correspondingly.&lt;/p&gt;

&lt;p&gt;Now let's dissect each invocation style.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Function invocation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The function invocation is the most familiar one. And also simple to understand.
A function, when invoked directly by its name and a set of paranthesis&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hey there !&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In the case of a plain function call, &lt;code&gt;this&lt;/code&gt; will always default to the global object.
But this is true only when the code is running inside &lt;strong&gt;non-strict mode&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  NOTE:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;ES5&lt;/strong&gt; introduced a new mode to JS called &lt;strong&gt;strict mode&lt;/strong&gt;. As the name suggests, running the JS code in strict mode will not allow a few things that otherwise run normally in &lt;strong&gt;non-strict&lt;/strong&gt; mode&lt;/p&gt;

&lt;p&gt;And one such thing is defaulting to the &lt;strong&gt;global object&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;So if the code is running in &lt;strong&gt;strict&lt;/strong&gt; mode, &lt;code&gt;this&lt;/code&gt; will not be defaulted to the &lt;strong&gt;global object&lt;/strong&gt;, and it will be &lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Standard Rule&lt;/strong&gt;: In the case of function invocation, the value of &lt;code&gt;this&lt;/code&gt; would be the &lt;strong&gt;global object&lt;/strong&gt; if running in &lt;strong&gt;non-strict&lt;/strong&gt; mode and &lt;code&gt;undefined&lt;/code&gt; when running in &lt;strong&gt;strict&lt;/strong&gt; mode&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// prints the global object of Node when running in true global scope of Node. For ex: in Node REPL&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;f2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;f2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output in the 1st case would be the Node.js &lt;code&gt;global object&lt;/code&gt;, whereas in the 2nd case, the output would be &lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Method invocation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is the case when a function is present inside an object. And to invoke that function, we need the object reference.
Ex:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; says hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Standard Rule&lt;/strong&gt;: In the case of method invocation, &lt;code&gt;this&lt;/code&gt; will become the &lt;strong&gt;object on which the function is called&lt;/strong&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So in our above example, since wish is called on &lt;strong&gt;obj&lt;/strong&gt;, inside wish, &lt;code&gt;this&lt;/code&gt; will point to the &lt;strong&gt;obj&lt;/strong&gt; itself, and therefore &lt;code&gt;this.name&lt;/code&gt; will have the value &lt;code&gt;Peter&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can also verify this by printing &lt;code&gt;this&lt;/code&gt; inside the wish method. It will give the object itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// `this` will point to the obj itself&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; says hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Gotcha:&lt;/strong&gt;&lt;br&gt;
Just think of what the output of the following code snippet would be ?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; says hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have guessed &lt;code&gt;undefined&lt;/code&gt;, good job; you have guessed it right.&lt;/p&gt;

&lt;p&gt;Now why's that? &lt;/p&gt;

&lt;p&gt;Always remember the &lt;strong&gt;golden rule&lt;/strong&gt;. &lt;code&gt;this&lt;/code&gt; inside a function will always get its value based on &lt;strong&gt;how it is invoked&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In our above example, it doesn't matter that the &lt;code&gt;wish&lt;/code&gt; variable is getting its value from &lt;code&gt;obj.wish&lt;/code&gt;, what matters is how the function is invoked at the end of the day. Here it is, a &lt;strong&gt;plain invocation&lt;/strong&gt; (Case 1)&lt;/p&gt;

&lt;p&gt;So in the case of plain invocation, we need to check whether the code is running in &lt;strong&gt;strict mode&lt;/strong&gt; or not. Since we aren't using &lt;strong&gt;'use strict;'&lt;/strong&gt;, our program is running in &lt;strong&gt;non-strict&lt;/strong&gt; mode, and &lt;code&gt;this&lt;/code&gt; will default to the global object.&lt;/p&gt;

&lt;p&gt;Now, based on the host, the &lt;strong&gt;global object&lt;/strong&gt; would differ. Assume the host is Node.js. In the Node.js global object, we do not have any property named &lt;code&gt;name&lt;/code&gt;, hence we get &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same explanation can be continued if the above program is running in a browser. In the browser, the global object is window, and the window object doesn't have any property named &lt;code&gt;name&lt;/code&gt;, hence even in the browser, &lt;code&gt;this.name&lt;/code&gt; will yield to &lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the same code had run in &lt;strong&gt;strict mode&lt;/strong&gt;, it would have led to a &lt;strong&gt;runtime error&lt;/strong&gt;. And I hope you can guess the reason. As in &lt;strong&gt;strict mode&lt;/strong&gt;, &lt;code&gt;this&lt;/code&gt; becomes &lt;code&gt;undefined&lt;/code&gt;, and accessing &lt;code&gt;.name&lt;/code&gt; on undefined would lead to a runtime error&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Explicit Binding&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is a special case where we bind the &lt;code&gt;this&lt;/code&gt; value to a specific value and then invoke the function with our given &lt;code&gt;this&lt;/code&gt;. 
(This might sound confusing at first glance but will be cleared very soon.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And to do this binding, JS has given us 3 methods.&lt;br&gt;
They are: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Call&lt;/li&gt;
&lt;li&gt;Apply&lt;/li&gt;
&lt;li&gt;Bind&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These 3 methods are available to every &lt;strong&gt;custom function&lt;/strong&gt; we define and help us to invoke a function with &lt;strong&gt;custom&lt;/strong&gt; &lt;code&gt;this&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;demo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is a demo function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;demo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not focus on the &lt;code&gt;null&lt;/code&gt; that is passed, but the point being, we can invoke the &lt;code&gt;call&lt;/code&gt; method on demo.&lt;/p&gt;

&lt;p&gt;Now let's see in detail about each of these methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;i) Call:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Syntax: &lt;br&gt;
&lt;code&gt;call(this, arg1, arg2, ...)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; says &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tony&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hey there&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, as you can see, we have reused the same function by passing different contexts.&lt;/p&gt;

&lt;p&gt;For the first console log, we get &lt;code&gt;Peter says hi&lt;/code&gt;&lt;br&gt;
And for the second console log, we get &lt;code&gt;Tony says hey there&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;first argument&lt;/em&gt; we pass to &lt;code&gt;call&lt;/code&gt; is considered as &lt;code&gt;this&lt;/code&gt; and same would be used inside the function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ii) Apply:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;"Apply" is the same as "call." Just that the way arguments are passed to functions differs.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;code&gt;apply(this, [arg1, arg2,...])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; says &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tony&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hey there&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the only difference is that the argument list is passed in an array instead of individual values&lt;/p&gt;

&lt;p&gt;After the introduction of the &lt;strong&gt;spread operator&lt;/strong&gt; in &lt;strong&gt;ES6&lt;/strong&gt;, all the places where &lt;code&gt;apply&lt;/code&gt; is used can be swapped with &lt;code&gt;call&lt;/code&gt; using the spread operator&lt;br&gt;
Ex: &lt;code&gt;wish.call(obj1, ...["hi"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;iii) Bind:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Bind is the most interesting of all. In the case of &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt;, the function is invoked &lt;strong&gt;immediately&lt;/strong&gt; with the context we pass.&lt;br&gt;
But in the case of bind, the function isn't invoked immediately; rather, bind &lt;strong&gt;returns a new function&lt;/strong&gt; with the passed context, and whenever this new function is invoked, the original function runs with the &lt;strong&gt;bound context&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;code&gt;bind(this, arg1, arg2, ...)&lt;/code&gt; =&amp;gt; returns a new function&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;boundGreet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns a new function and the returned function will always have `this` pointed to `person`&lt;/span&gt;

&lt;span class="nf"&gt;boundGreet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hello, John&lt;/span&gt;

&lt;span class="c1"&gt;// At the end function greet runs upon boundGreet(), but `this` is binded to person&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  NOTE:
&lt;/h2&gt;

&lt;p&gt;Bind permanently binds &lt;code&gt;this&lt;/code&gt; context, meaning you cannot override the context later.&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mike&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// fn has `this` permanently binded to person, and cannot be overridden, hence we see the output as Hello John&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bind can be very useful in the case of callback functions so that the callback functions don't lose the &lt;code&gt;this&lt;/code&gt; reference.&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hi, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try to guess the output of the above code snippet. The answer might surprise you at first.&lt;br&gt;
The output of the code is &lt;code&gt;Hi undefined&lt;/code&gt; if running in &lt;strong&gt;non-strict&lt;/strong&gt; mode; else, a &lt;strong&gt;runtime error&lt;/strong&gt; if running in &lt;strong&gt;strict&lt;/strong&gt; mode&lt;/p&gt;

&lt;p&gt;You might say, Hey, this happens in the case of a &lt;strong&gt;plain invocation&lt;/strong&gt; call, and that's a perfect observation.&lt;/p&gt;

&lt;p&gt;Even though &lt;code&gt;sayHi&lt;/code&gt; is invoked on the user object &lt;strong&gt;(method invocation - case 2),&lt;/strong&gt; what we have to observe is that it is passed as a &lt;strong&gt;callback function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And now &lt;strong&gt;setTimeout&lt;/strong&gt; has full &lt;strong&gt;control&lt;/strong&gt; over this &lt;strong&gt;callback function&lt;/strong&gt;, and once the timer &lt;strong&gt;expires&lt;/strong&gt; (which is 1000 ms in this case), &lt;strong&gt;setTimeout will invoke our callback function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;setTimeout would roughly look like (not exact code, but generic pseudo code)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="cm"&gt;/*
      Register the timer in node process and wait till timer expires
   */&lt;/span&gt;

   &lt;span class="c1"&gt;// once the timer expires,&lt;/span&gt;
   &lt;span class="nf"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// this is the most important thing to observe, now try to guess what this is ? This is the plain function invocation (Case 1)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's the reason the output we saw in the earlier code snippet produced output as if it was invoked directly (&lt;strong&gt;plain invocation&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;Now if I ask you to fix this code so that when the callback runs, we should get the output as &lt;code&gt;Hi, Peter&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It's simple if we ask what we need?&lt;/p&gt;

&lt;p&gt;We need a function that won't &lt;strong&gt;execute immediately&lt;/strong&gt; and also has a &lt;strong&gt;permanent this binding&lt;/strong&gt;. And that's what &lt;code&gt;bind&lt;/code&gt; does.&lt;/p&gt;

&lt;p&gt;Fixing the above code snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hi, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sayHi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// now fn has a permanent this binding to user and sayHi won't execute immediately at this moment of `bind`&lt;/span&gt;
&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hi, Peter&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Gotcha&lt;/strong&gt;:&lt;br&gt;
Remember we left a question unanswered. Why does &lt;code&gt;this&lt;/code&gt; give &lt;strong&gt;{}&lt;/strong&gt; as a result in a Node.js file?&lt;/p&gt;

&lt;p&gt;As we already discussed, this is because of Node.js behavior; our code isn't running in &lt;strong&gt;true global scope&lt;/strong&gt;, as Node.js &lt;strong&gt;wraps&lt;/strong&gt; our file inside a function and internally calls this function using the &lt;code&gt;call&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;And to this &lt;code&gt;call&lt;/code&gt; method, the first argument passed is module.exports. Initially, when a file is run, module.exports is empty ({})&lt;/p&gt;

&lt;p&gt;And we also know that the first argument to &lt;code&gt;call&lt;/code&gt; is the &lt;code&gt;this&lt;/code&gt; we set, and this is the reason why &lt;code&gt;this&lt;/code&gt; gives an empty object in a Node.js file.&lt;/p&gt;

&lt;p&gt;Under the hood, code is wrapped in a function. And that function is called via &lt;code&gt;call(module.exports,arg1,arg2 etc)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So a file like this in Node.js becomes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;__filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// and the above function is invoked by Node in the following way&lt;/span&gt;
&lt;span class="cm"&gt;/*
wrapperFunction.call(
  module.exports,   // this ({})
  module.exports,   // exports
  require,          // require
  module,           // module
  __filename,       // filename
  __dirname         // dirname
);
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Constructor invocation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The final way to invoke a function is to invoke it using the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;code&gt;new fn()&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The new keyword broadly does 4 things in the background. They are:&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Create a brand new object&lt;/li&gt;
&lt;li&gt;Make &lt;code&gt;this&lt;/code&gt; point to this new object&lt;/li&gt;
&lt;li&gt;Set the new object's prototype (&lt;strong&gt;proto&lt;/strong&gt; property) to the function's prototype&lt;/li&gt;
&lt;li&gt;Run the constructor function and return this new object (inbuilt return) if no explicit return is mentioned.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Meaning now &lt;code&gt;this&lt;/code&gt; is pointed to the &lt;strong&gt;object created&lt;/strong&gt; because of the &lt;code&gt;new&lt;/code&gt; keyword&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Peter&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// 25&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Note:
&lt;/h2&gt;

&lt;p&gt;To understand the &lt;strong&gt;constructor invocation&lt;/strong&gt; completely, we also need to have knowledge of &lt;strong&gt;objects&lt;/strong&gt; and &lt;strong&gt;prototypical inheritance&lt;/strong&gt;, but we will skip this in this blog, as this is purely focused on the &lt;code&gt;this&lt;/code&gt; keyword&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the instruction &lt;code&gt;const u = new User("Peter", 25)&lt;/code&gt; runs, all the above &lt;strong&gt;4 activities mentioned&lt;/strong&gt; will run, and finally an object will be returned from the user function (internally), and &lt;strong&gt;variable &lt;code&gt;u&lt;/code&gt; now holds the reference to the object&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Gotcha
&lt;/h2&gt;

&lt;p&gt;What will happen if we call the user function directly without the new keyword?&lt;/p&gt;

&lt;p&gt;This will lead to &lt;strong&gt;plain function invocation&lt;/strong&gt; (Case 1), and depending on whether the code is running in &lt;strong&gt;strict&lt;/strong&gt; vs. &lt;strong&gt;non-strict&lt;/strong&gt; mode, the output might differ.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;strict&lt;/strong&gt; mode, the program will run into a runtime error, as &lt;code&gt;this&lt;/code&gt; is &lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But in &lt;strong&gt;non-strict&lt;/strong&gt; mode, &lt;code&gt;this&lt;/code&gt; will default to the &lt;strong&gt;global object,&lt;/strong&gt; and &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; properties will be added to the &lt;strong&gt;global object&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;u&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;           &lt;span class="c1"&gt;// undefined. without new keyword, there is no implicit return, hence default value undefined is returned&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;globalThis&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// (globalThis refers to global object of the host(Browser/Node.js etc)) now globalThis will have name, age properties on it as code is running in non-strict mode by default&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;So these are the 4 cases inside functions where the &lt;code&gt;this&lt;/code&gt; value always changed because of how the function was invoked.&lt;/p&gt;




&lt;h2&gt;
  
  
  Special Cases:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. &lt;u&gt;Arrow function&lt;/u&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ES6&lt;/strong&gt; introduced a new type of function named arrow functions.&lt;br&gt;
The 2 main reasons why arrow functions were introduced were:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To reduce verbose code&lt;/li&gt;
&lt;li&gt;To hold &lt;code&gt;this&lt;/code&gt; based on the place they were declared&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now in the blog, we will focus on the use case related to the &lt;code&gt;this&lt;/code&gt; keyword&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Golden rule&lt;/strong&gt;: Arrow functions always &lt;strong&gt;capture&lt;/strong&gt; &lt;code&gt;this&lt;/code&gt; at the &lt;strong&gt;time of creation&lt;/strong&gt; from the &lt;strong&gt;nearest enclosing execution context&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;In other words, &lt;strong&gt;arrow functions do not have their own &lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;code&gt;() =&amp;gt; {}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's understand the above statement in detail. But before that, let's understand the problem that existed in the first place&lt;/p&gt;

&lt;p&gt;Now we know that every function in JS has an execution context, which has an impact on the &lt;code&gt;this&lt;/code&gt; value&lt;/p&gt;

&lt;p&gt;Take the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we know that when the &lt;code&gt;greet&lt;/code&gt; function is invoked, first the timer runs; post &lt;strong&gt;timer expiry&lt;/strong&gt;, the &lt;strong&gt;callback&lt;/strong&gt; function will be invoked as a &lt;strong&gt;plain function call&lt;/strong&gt; from the &lt;code&gt;setTimeout&lt;/code&gt; function, which leads &lt;code&gt;this&lt;/code&gt; to either take the &lt;strong&gt;global object&lt;/strong&gt; in &lt;strong&gt;non-strict&lt;/strong&gt; mode or &lt;code&gt;undefined&lt;/code&gt; in &lt;strong&gt;strict&lt;/strong&gt; mode.&lt;/p&gt;

&lt;p&gt;And we know &lt;code&gt;bind&lt;/code&gt; is one solution to solve this problem.&lt;/p&gt;

&lt;p&gt;But arrow functions also solve this problem, that too effortlessly compared to &lt;code&gt;bind&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Rewriting the same example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now remembering the &lt;strong&gt;golden rule&lt;/strong&gt;, arrow functions capture &lt;code&gt;this&lt;/code&gt; on the &lt;strong&gt;moment of creation&lt;/strong&gt; from the &lt;strong&gt;nearest enclosing execution context&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So the &lt;strong&gt;nearest enclosing execution context&lt;/strong&gt; to our arrow function is the &lt;code&gt;greet&lt;/code&gt; function (as functions in JS create execution contexts).&lt;br&gt;
So at the time of creation itself (during the &lt;strong&gt;setTimeout instruction&lt;/strong&gt;), the arrow function will &lt;strong&gt;capture&lt;/strong&gt; &lt;code&gt;this&lt;/code&gt; from the &lt;code&gt;greet&lt;/code&gt; function&lt;/p&gt;

&lt;p&gt;Now what would be the value of &lt;code&gt;this&lt;/code&gt; in the &lt;code&gt;greet&lt;/code&gt; function? &lt;br&gt;
That depends on how the &lt;code&gt;greet&lt;/code&gt; function is invoked. It's invoked by user reference (Case 2), so &lt;code&gt;this&lt;/code&gt; will point to the user object itself.&lt;br&gt;
And this &lt;code&gt;this&lt;/code&gt; value is captured by the arrow function.&lt;/p&gt;

&lt;p&gt;So when the arrow function actually runs after the timer expires, it already has &lt;code&gt;this&lt;/code&gt; with it (&lt;strong&gt;captured at the time of creation&lt;/strong&gt;), and &lt;code&gt;this.name&lt;/code&gt; prints &lt;code&gt;Peter&lt;/code&gt; after 1 ms.&lt;/p&gt;

&lt;p&gt;Example 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hi &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try to guess the output. (Assume the host is Browser.)&lt;/p&gt;

&lt;p&gt;If you guessed the output as &lt;code&gt;Hi, undefined&lt;/code&gt;, excellent, you have understood &lt;code&gt;this&lt;/code&gt; in arrow functions.&lt;/p&gt;

&lt;p&gt;Again, to answer this, let's remember the golden rule: arrow functions capture this at the &lt;strong&gt;moment of creation&lt;/strong&gt; from the &lt;strong&gt;nearest enclosing execution context&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To the wish function, the &lt;strong&gt;nearest enclosing execution context&lt;/strong&gt; is the &lt;strong&gt;global execution context&lt;/strong&gt;, and in the &lt;strong&gt;GEC&lt;/strong&gt;, &lt;code&gt;this&lt;/code&gt; holds the value of the &lt;strong&gt;global object&lt;/strong&gt;. And since here the host is a browser, the global object is the window object.&lt;/p&gt;

&lt;p&gt;And in the window object, we do not have any property named &lt;code&gt;name&lt;/code&gt;, hence &lt;code&gt;.name&lt;/code&gt; gives &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exercise&lt;/strong&gt;:&lt;br&gt;
Try to guess the output of the same program when the host is Node.js (when run in true global scope vs. when run via file)&lt;/p&gt;

&lt;p&gt;Answer: In both cases, &lt;code&gt;.name&lt;/code&gt; is undefined, but in true global scope, &lt;code&gt;this&lt;/code&gt; holds the value of the node's &lt;strong&gt;global object&lt;/strong&gt;&lt;br&gt;
And when run via file, &lt;code&gt;this&lt;/code&gt; has the value of an &lt;strong&gt;empty object&lt;/strong&gt; ({}).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. &lt;u&gt;Combination of invocations in a functional execution context:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we know that &lt;code&gt;this&lt;/code&gt; inside a function gets a value based on how the &lt;strong&gt;function is invoked&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And we also saw that there are broadly &lt;strong&gt;4 ways&lt;/strong&gt; to invoke a function in JS.&lt;/p&gt;

&lt;p&gt;But what happens when a function is invoked using a &lt;strong&gt;combination&lt;/strong&gt; of these four approaches?&lt;/p&gt;

&lt;p&gt;Let's take an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Peter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; says &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tony&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Steve&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hey there&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//combination of case 2 and case 3&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bound&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// combo of case 2 and case 3&lt;/span&gt;
&lt;span class="nf"&gt;bound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;on your left&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now what will be the output of this program? &lt;/p&gt;

&lt;p&gt;If we observe, the &lt;code&gt;wish&lt;/code&gt; function is invoked by a &lt;strong&gt;combination&lt;/strong&gt; of &lt;strong&gt;method invocation&lt;/strong&gt; using object reference (case 2) and &lt;strong&gt;explicit binding&lt;/strong&gt; (case 3).&lt;/p&gt;

&lt;p&gt;Now what should be the value of &lt;code&gt;this&lt;/code&gt; ? There's a conflict.&lt;/p&gt;

&lt;p&gt;To resolve these conflicts, &lt;strong&gt;ECMA&lt;/strong&gt; has given a precedence order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Precedence order&lt;/strong&gt;:&lt;br&gt;
&lt;strong&gt;Constructor invocation &amp;gt; Explicit binding &amp;gt; Method invocation &amp;gt; Plain invocation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So according to the &lt;strong&gt;binding precedence&lt;/strong&gt; rules, &lt;strong&gt;explicit binding&lt;/strong&gt; (using call, apply, or bind) takes priority over &lt;strong&gt;method invocation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Hence, the output of the above program would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Tony says hey there
Steve says on your left
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ex 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bound Object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BoundPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Using `new`&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BoundPerson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Actual Person&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ideally, &lt;code&gt;bind&lt;/code&gt; creates a permanent binding, making &lt;code&gt;boundPerson&lt;/code&gt; function to point &lt;code&gt;this&lt;/code&gt; to &lt;code&gt;obj&lt;/code&gt;, but as per the &lt;strong&gt;precedence rules&lt;/strong&gt;, the &lt;code&gt;new&lt;/code&gt; operator takes more precedence.&lt;/p&gt;

&lt;p&gt;And hence, &lt;code&gt;this&lt;/code&gt; will point to the object returned by the &lt;code&gt;new&lt;/code&gt; keyword, leading to the output &lt;code&gt;Actual Person&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exercise&lt;/strong&gt;: Try running the 4 steps that run in the background when the &lt;code&gt;new&lt;/code&gt; operator is used&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. &lt;u&gt;Module scope:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Remember at the start of the blog, we learned that JS has &lt;strong&gt;4 execution contexts&lt;/strong&gt; defined in the language&lt;/p&gt;

&lt;p&gt;And we discussed in detail regarding &lt;code&gt;this&lt;/code&gt; behavior in &lt;strong&gt;Global Execution Context&lt;/strong&gt; (GEC) and &lt;strong&gt;Functional Execution Context&lt;/strong&gt; (FEC).&lt;/p&gt;

&lt;p&gt;The remaining contexts are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Eval&lt;/li&gt;
&lt;li&gt;Module&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We’ll skip &lt;code&gt;eval&lt;/code&gt;, since its use is widely discouraged in modern JavaScript.&lt;/p&gt;

&lt;p&gt;Understanding &lt;code&gt;this&lt;/code&gt; in &lt;strong&gt;module&lt;/strong&gt; scope is pretty easy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Module scope&lt;/strong&gt;, introduced in &lt;strong&gt;ES6&lt;/strong&gt;, ensures that each JavaScript module has its own &lt;strong&gt;isolated&lt;/strong&gt; scope, separate from other files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Standard rule&lt;/strong&gt;: &lt;strong&gt;Modules&lt;/strong&gt; always run in &lt;strong&gt;strict mode&lt;/strong&gt; by deafult.&lt;/p&gt;

&lt;p&gt;Meaning we don't have to explicitly mention "use strict";&lt;/p&gt;

&lt;p&gt;If we recollect the case-1 of FEC, i.e., the &lt;strong&gt;plain invocation&lt;/strong&gt;, &lt;code&gt;this&lt;/code&gt; always had a possibility of 2 values. &lt;br&gt;
&lt;strong&gt;Global object&lt;/strong&gt; in &lt;strong&gt;non-strict&lt;/strong&gt; mode and &lt;code&gt;undefined&lt;/code&gt; in &lt;strong&gt;strict&lt;/strong&gt; mode.&lt;/p&gt;

&lt;p&gt;But running the same file as a module cuts down the option to default to a &lt;strong&gt;global object&lt;/strong&gt;, as they always run in &lt;strong&gt;strict&lt;/strong&gt; mode&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;wish&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; says hi`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now try to guess the output of this program. &lt;br&gt;
We have spent a lot of time understanding &lt;code&gt;this&lt;/code&gt; in &lt;strong&gt;FEC&lt;/strong&gt;. By this time, you could easily spot that this is &lt;strong&gt;plain invocation&lt;/strong&gt; (case 1)&lt;/p&gt;

&lt;p&gt;In case 1, we always have the scope to get 2 answers: either a &lt;strong&gt;global object&lt;/strong&gt; (if running in non-strict mode) or &lt;code&gt;undefined&lt;/code&gt; (if running in strict mode).&lt;/p&gt;

&lt;p&gt;Now, since we know that &lt;code&gt;modules&lt;/code&gt; by default runs in &lt;strong&gt;strict mode&lt;/strong&gt;, the output is &lt;code&gt;this&lt;/code&gt; becomes &lt;code&gt;undefined&lt;/code&gt; and access to &lt;code&gt;.name&lt;/code&gt; on &lt;code&gt;undefined&lt;/code&gt; leads to a runtime error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;:&lt;br&gt;
In JS, we can create a module by naming the file with the &lt;code&gt;.mjs&lt;/code&gt; extension&lt;br&gt;
Alternatively, by setting the &lt;code&gt;type&lt;/code&gt; property to "module" in &lt;code&gt;package.json&lt;/code&gt;, the entire project is treated as &lt;strong&gt;ES modules&lt;/strong&gt; and runs in &lt;strong&gt;strict&lt;/strong&gt; mode.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>this</category>
    </item>
  </channel>
</rss>
