{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/a-beginners-guide-to-prototypes-and-prototypal-inheritance-in-javascript/",
    "result": {"data":{"markdownRemark":{"html":"<p>We know that in JavaScript, everything (<em>except the primitive data types</em>) is an object. An Object in an object. A function is an object. An array is an object. And so on.</p>\n<p>Also in JavaScript, every object is linked to another object. That another object is called prototype. The linkage happens through an internal hidden property known as <code>__proto__</code> (<em>double underscore each side</em>), which gets assigned to the object during its creation by default. The prototype also gets assigned to the <code>__proto__</code> property by default.</p>\n<p>More importantly, the created object inherits properties and methods from its prototype (<em>aka prototypal inheritance</em>). Also, every object in JavaScript has <del>Object.prototype</del> (<em>an object that comes built-in with JavaScript</em>) at the top of its prototype chain.</p>\n<p>This all might sound gibberish to you. In order to make this a bit clearer, let’s look at some examples.</p>\n<p>First off, we will create an object using the literal notation using curly braces. Note that you can also create an object using constructor notation using the <del>new</del> operator.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"js\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">const</span><span class=\"mtk1\"> </span><span class=\"mtk3\">person</span><span class=\"mtk1\"> </span><span class=\"mtk12\">=</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk3\">firstName</span><span class=\"mtk14\">:</span><span class=\"mtk1\"> </span><span class=\"mtk16\">&quot;Phillip&quot;</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk3\">lastName</span><span class=\"mtk14\">:</span><span class=\"mtk1\"> </span><span class=\"mtk16\">&quot;Schofield&quot;</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>Nothing fancy here. We have a simple <del>person</del> object which has two properties - <del>firstName</del> &#x26; <del>lastName</del> - defined inside it.</p>\n<p>We can check whether a property is present in a specified object using the <del>in</del> operator. The <del>in</del> operator returns <del>true</del> if the specified property is defined in the specified object or its prototype chain.</p>\n<p>Therefore, the following code returns true because the <del>person</del> object does have a <del>firstName</del> property.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"js\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk16\">&quot;firstName&quot;</span><span class=\"mtk1\"> </span><span class=\"mtk10\">in</span><span class=\"mtk1\"> </span><span class=\"mtk3\">person</span><span class=\"mtk1\"> </span><span class=\"mtk4 mtki\">// true</span></span></span></code></pre>\n<p>Now, let’s consider another example.</p>\n<p>What do you think the following code will return as its output?</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"js\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">“</span><span class=\"mtk3\">toString</span><span class=\"mtk1\">” </span><span class=\"mtk10\">in</span><span class=\"mtk1\"> </span><span class=\"mtk3\">person</span></span></span></code></pre>\n<p>At first glance, you might say that the output will be <del>false</del> because the <del>person</del> object does not have a <del>toString</del> property. But lo and behold, we get <del>true</del> as our output.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 471px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/5d1d0e4a60594f774949bdfb68125354/27c0d/prototype-0.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 57.00000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABZElEQVQoz42OyW7cMBBE9f8fmEwCjDyjhU2xSXHfFxljJ7cg9jt2oarfhIgAIKVSShMCiNxaBwDHcThrhRCcc2OMlCrnNBOtjV2WJ0dmrZmWZXk8nwBAKQAQzjlh8vbGftzpCny+3wEAEV93sj+Wbd32fVsfG6MoJsaQkUOgEEKFEGOMPqVUa4hRa00pdc4ppay1jOF5vkScNSi91G7qvQ/X7OmWVawL9UZXpargzbvrK6YxxnVdSsl5vs9vczCmAKRtS4j9I/oXr2CMaxp99NJSSI/H83b7RQicQnTvq5TtpSC6swVlltFaK85TIPZaPxem0Wo/VdXRGImcaYHBm6vVUeqodZQyWus599pba6XWkvPo/Y92KUOZKmRyYTjfQxw+jFzG9Q0mAomtUmwnriencd81Ab1u4e/6f8vbHgXVRevubc/NO59S2PZY69fPp5WEn78NOSqwCjRTVuAonJfvaL8DM/R24NqUdnMAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"prototype &amp; protypal inheritance in JavaScript\"\n        title=\"prototype &amp; protypal inheritance in JavaScript\"\n        src=\"/static/5d1d0e4a60594f774949bdfb68125354/27c0d/prototype-0.png\"\n        srcset=\"/static/5d1d0e4a60594f774949bdfb68125354/56d15/prototype-0.png 200w,\n/static/5d1d0e4a60594f774949bdfb68125354/d9f49/prototype-0.png 400w,\n/static/5d1d0e4a60594f774949bdfb68125354/27c0d/prototype-0.png 471w\"\n        sizes=\"(max-width: 471px) 100vw, 471px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>What the heck is happening here?</p>\n<p>We have already said that an object inherits properties and methods from its prototype. And the object can find its prototype (<em>which is also an object</em>) through its internal <code>__proto__</code> property (<em>hidden</em>).</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 468px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/21a42bb832c5fd0e00cc185353946cfc/b4003/prototype-1.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 67.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAIAAACgpqunAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABk0lEQVQoz52RS4vbMBSF/f9/VBfddFPKLIYSkpCMHFuWrLeurJf1KMmipAOlod9Kuuice8QZCCFCCACgdF1XBgBKSrIsYI0xljFujFVKO+fA+Qs2m4Pr9cNoBQDDOI7zPDO2zvNMKVm5Oo78/YwvE59njBBijAkhOOeEkI8bJoSMI7phJqQaKKEEE6WU1trHCJuHLYDzUumHHVVKS6k459M0UbIsC7HWIGKktkOGBMRQaujqNm2TNS3G1lp/gWFzbhzR4XDgnGdK/Onkr5cSfO/92aI98bg+xD7cyTn/nrVS+oubrRSaMy14zaWUmnMutfbXGLy0Sepd8OKzM04qGUP4/OovQQYuNwuh/xcD54wJvgUf91xSLjHWlFupKSXnXEqp5VwAyrbtIfhYn0MMe4peWoOFxrJaW+axzrcdrA/BWhtibCnuWhdrsnPg/xTLVK+TO/5Up7OeuEVndDkiYOL+03/G/obhDfsfyL1h/x2br8fpy1VOsPfea3202j51/CSGkHzpoXa/N19arPdzeanm/gutwChf8Xm85gAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"prototype &amp; protypal inheritance in JavaScript\"\n        title=\"prototype &amp; protypal inheritance in JavaScript\"\n        src=\"/static/21a42bb832c5fd0e00cc185353946cfc/b4003/prototype-1.png\"\n        srcset=\"/static/21a42bb832c5fd0e00cc185353946cfc/56d15/prototype-1.png 200w,\n/static/21a42bb832c5fd0e00cc185353946cfc/d9f49/prototype-1.png 400w,\n/static/21a42bb832c5fd0e00cc185353946cfc/b4003/prototype-1.png 468w\"\n        sizes=\"(max-width: 468px) 100vw, 468px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>JavaScript does not find the <del>toString</del> property defined inside the <del>person</del> object. Therefore it moves up the prototype chain looking for the property and finds it defined inside the <del>person</del> object’s prototype, which in our case is <del>Object.prototype</del> - an object that sits at the top of the prototype chain (<em>that is its prototype is null</em>) and comes standard with JavaScript. This is the reason why the console logs in <del>true</del> as the output.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 470px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/90018b0ec430be08b8ce3a0e3f06c0f8/c52b7/prototype-3.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 115.00000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAXCAIAAACEf/j0AAAACXBIWXMAAA7DAAAOwwHHb6hkAAACdklEQVQ4y5WU647bIBCF8/4vuFK1bTZ2bGMD5jYwM4xxKifaNtt2q/YT4o99YC6HOWltpkl5H5zz0zRZa1NK86y0XjKAvRNC8N4T0bfR+5C6rjNaA8Cp67q3tzelpnlWSk3WmF65l7N6Oc+jMufzt2VZzB2lVHedpkld+74bZmPdSSk1T7PRxloXI+RSUkZAChGMXYdhSClZu4YQhmHQepnnOQQ/mmR9OrW94ZrduPZX6yygNRL8hnj7B0632y2EcLlcXr++Yow8DHg+k1KPz/u+//h1f+eneNu2BBmZZdtu/8mpMoNe4jyDtY23fOf5+L+Kq0QDAqVB2kgyQAbYW/sR5+1pHduzGIkW4yGX2/9zqpWN1t477x0SSgzifcvAzP7OxizOVXN0IUCt28dqC0ucknm1forV6nI+Y98JcxWpIntre62NuUmt0tr+i7hKsRiuiRK1ksX7zfubyD/1mYhKKBQYIwtvxNt6WN0h4s/e/tbk95tFwGdcCR3hiiWSWWPf99M4aq2VUiGET28WEYyFA5MnNAUDlUjBOq3Xrusul0sIIaUU7xDRB3EVCS7DSmAxrwhr8Ssa7ebFXq/HOxqGIcYIACklZv4gTlQvSzKG1FKGFV+H5Us/vbxdTeaHVdrdM38Ou4r4WLzG1RZjy2xcN86Li7lgubuViGqt22/mf+RcueCR8IpoikBlEkQZx+GR8ziOIYTngD8UjPAodbElm8KRsTBktqvt+77rukfBn0v1HHbNIddY0RN6YuCSq/fxMTceMwwAPgl7E3BZslAgCoSZY8JlVo95uH3+zk98uHBvW9vb3qQd62CvVWqtIrI/8Yv4O2OBMdfhiKPlAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"prototype &amp; protypal inheritance in JavaScript\"\n        title=\"prototype &amp; protypal inheritance in JavaScript\"\n        src=\"/static/90018b0ec430be08b8ce3a0e3f06c0f8/c52b7/prototype-3.png\"\n        srcset=\"/static/90018b0ec430be08b8ce3a0e3f06c0f8/56d15/prototype-3.png 200w,\n/static/90018b0ec430be08b8ce3a0e3f06c0f8/d9f49/prototype-3.png 400w,\n/static/90018b0ec430be08b8ce3a0e3f06c0f8/c52b7/prototype-3.png 470w\"\n        sizes=\"(max-width: 470px) 100vw, 470px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>This was a simple example. We did not have any intermediate object that the <del>person</del> object inherited from. The <del>person</del> object inherited properties and methods directly from the <del>Object.prototype</del> object.</p>\n<p>It is important to note that we can also assign our own prototype. Let’s do that by introducing another prototype object called <del>person</del> that other objects will inherit properties and methods from.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"js\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk10\">const</span><span class=\"mtk1\"> </span><span class=\"mtk3\">person</span><span class=\"mtk1\"> </span><span class=\"mtk12\">=</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk3\">firstName</span><span class=\"mtk14\">:</span><span class=\"mtk1\"> </span><span class=\"mtk16\">&quot;Phillip&quot;</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk3\">lastName</span><span class=\"mtk14\">:</span><span class=\"mtk1\"> </span><span class=\"mtk16\">&quot;Schofield&quot;</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"4\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk6\">fullName</span><span class=\"mtk1\">() {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"5\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk9\">console</span><span class=\"mtk1\">.</span><span class=\"mtk6\">log</span><span class=\"mtk1\">(</span><span class=\"mtk16\">`My full name is </span><span class=\"mtk8\">${</span><span class=\"mtk9 mtkb\">this</span><span class=\"mtk14\">.</span><span class=\"mtk7\">firstName</span><span class=\"mtk8\">}</span><span class=\"mtk16\">  </span><span class=\"mtk8\">${</span><span class=\"mtk9 mtkb\">this</span><span class=\"mtk14\">.</span><span class=\"mtk7\">lastName</span><span class=\"mtk8\">}</span><span class=\"mtk16\">`</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"6\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">  },</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"7\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span></code></pre>\n<p>Now, let’s create a child of the <del>person</del> object using <del>Object.create()</del> function, which is built-in JavaScript and is used to create children from a prototype object.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"js\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk10\">const</span><span class=\"mtk1\"> </span><span class=\"mtk3\">personOne</span><span class=\"mtk1\"> </span><span class=\"mtk12\">=</span><span class=\"mtk1\"> </span><span class=\"mtk9\">Object</span><span class=\"mtk1\">.</span><span class=\"mtk6\">create</span><span class=\"mtk1\">(</span><span class=\"mtk3\">person</span><span class=\"mtk1\">)</span></span></span></code></pre>\n<p>Here we have created a child object called <del>personOne</del>. Because children inherit the properties and methods from their prototype object, <del>personOne</del> has access to all the properties and methods from the <del>person</del> object. We can prove that by accessing the <del>firstName</del> property from the <del>personOne</del> object. As expected, we get <del>Phillip</del> as the output.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"js\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">personOne</span><span class=\"mtk1\">.</span><span class=\"mtk7\">firstName</span><span class=\"mtk1\"> </span><span class=\"mtk4 mtki\">// Phillip</span></span></span></code></pre>\n<p>Let’s see what we get if we log <del>personOne</del> object to the console.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 472px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/0a48eec79c6351c58203ce9ca7d58e05/85788/prototype-4.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 64%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABlklEQVQoz52SW2vcMBBG/f9/VPvax9BAaNpmvYm9uo6uI+tqycXQ0kCXFnoY9DDiaD4GTUJISqm1zhhLCJESEAOllHMeEJVSAOC9N8bUkmfqnd+WZRGcBfTTsizX1yvjjFJCKQGAG7dfZv50YSuD+XKhlEkJoIAQ8rbeboTcbuvrypnUkxCCEaZAgzIx5pjSllKqbUvJOUcpRURjrPcohFAKpJTonTQBbJj63js2p8Ky6HXlG7rmXDN6j/F4zzj+ZBrjbCutXl6+z9c5eV8JycuSpejjnnE+dF6McUzn0Ufc4jxfvj4/M8aM1iOEqnWztinVA1YwRSdE1FqDEHspvybXWqTNJikFhBLNOTo3Sjmr1pHzaK3HvOe91ppLySn1ff8pH8ex15ZSHuNo7QzU+7mHMUZ/V3fjT731KNCrTUKSMm+hondCyBCC1gYAjDHW2tbaHbnWcv4EyWP09zbzmzvym9k+XcXnV3h4kEo3xGZd87hr02Lqx1+ZvpnyKMMjyx8+akKT1pXQLGRZ1+R8+4fcx/Hf/AATOuuoAwCKWAAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"prototype &amp; protypal inheritance in JavaScript\"\n        title=\"prototype &amp; protypal inheritance in JavaScript\"\n        src=\"/static/0a48eec79c6351c58203ce9ca7d58e05/85788/prototype-4.png\"\n        srcset=\"/static/0a48eec79c6351c58203ce9ca7d58e05/56d15/prototype-4.png 200w,\n/static/0a48eec79c6351c58203ce9ca7d58e05/d9f49/prototype-4.png 400w,\n/static/0a48eec79c6351c58203ce9ca7d58e05/85788/prototype-4.png 472w\"\n        sizes=\"(max-width: 472px) 100vw, 472px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>We get an empty object.</p>\n<p>This is because we have not defined any properties and methods inside <del>personOne</del> object. However, <del>personOne</del> inherits properties and methods from its prototype object - the <del>person</del> object. As a result, <del>personOne.firstName</del> logs in <del>Phillip</del> to the console.</p>\n<p>We can also override the inherited properties and methods and create <del>personOne</del> object’s own properties and methods. We can also create new properties and methods.</p>\n<p>Let’s override the <del>firstName</del> &#x26; <del>lastName</del> properties using dot notation.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"js\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">personOne</span><span class=\"mtk1\">.</span><span class=\"mtk7\">firstName</span><span class=\"mtk1\"> </span><span class=\"mtk12\">=</span><span class=\"mtk1\"> </span><span class=\"mtk16\">&quot;Keira&quot;</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk3\">personOne</span><span class=\"mtk1\">.</span><span class=\"mtk7\">lastName</span><span class=\"mtk1\"> </span><span class=\"mtk12\">=</span><span class=\"mtk1\"> </span><span class=\"mtk16\">&quot;Knightley&quot;</span></span></span></code></pre>\n<p>Now <del>personOne</del> is no longer an empty object. It has its own <del>firstName</del> &#x26; <del>lastName</del> properties. If we log <del>personOne.fullName()</del> to the console, we get <del>My full name is Keira Knightey</del>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 474px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/918c6cf8c862643ae21564f1bd2f1911/823f8/prototype-2.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 73.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAIAAABr+ngCAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAB6klEQVQoz52S227kIAxA8/8f2Om2M7lMLg7EgCEkEG5ZTbddVdrpyx5ZFrJ0jGW5WpYFYNZaK0UAIISwdp0BOOfWWiklIhpjpFLh8A0Ype04DmwGu65V3/dt1zLGACYAQFxGRpeGvdxgmLFpagBYFkQhAKDrx2EYp3Hohhm4rDjnMIJEiajc7vd9t7t3IdjdEdE0TcasUipjDGMMl4VzbgyhslyuVY45m6DR9oOaRr6vJhmTSGXnzu+U81+qUh5lFHi7XuumdcaEcXD3zi88PxP+dCof3arHI5fNbnVdv729Mca0UmXbAlHUOiqZty1KfZC31hKRQEwhfP18HAdXntyCfBhHnMGQKseRvX9k50oIeduTSyEE59y+bTmlT/k8z+APt7ucS4w555If6XOwv/GUqqQSlHcmkk5CBO+KXVeOuDknSSMKpRQRxRifyDFGrZUQqDUZQ8vCtJbnsWeJ2ajzTOWLJzJp936d3t/HtuV1PV8uXd8jMcNf2uVXLyeK6celV3Wzdnfs7sswymGQ19vUD4Io6DWZNSmKMPsf5curutVz3XxEDV3Hr9eR8bXt+PU23e+i6915lo8dfg4fQgAApVSFcu86bFo2TaR1JAqKAukolRfSSemdT4+r+EbO+TiOEEKV8vnf/Aaz1Fow5qBvLQAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"prototype &amp; protypal inheritance in JavaScript\"\n        title=\"prototype &amp; protypal inheritance in JavaScript\"\n        src=\"/static/918c6cf8c862643ae21564f1bd2f1911/823f8/prototype-2.png\"\n        srcset=\"/static/918c6cf8c862643ae21564f1bd2f1911/56d15/prototype-2.png 200w,\n/static/918c6cf8c862643ae21564f1bd2f1911/d9f49/prototype-2.png 400w,\n/static/918c6cf8c862643ae21564f1bd2f1911/823f8/prototype-2.png 474w\"\n        sizes=\"(max-width: 474px) 100vw, 474px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Now let’s delete the <del>firstName</del> property from the <del>personOne</del> object, after which <del>personOne</del> object is left with only the <del>lastName</del> property.</p>\n<p>What do you think will be the output of <del>personOne.fullName()</del> ?</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 471px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/9006996c6eef52e6d097d2f29e97c35e/27c0d/prototype-6.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 83%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAIAAABSJhvpAAAACXBIWXMAAA7DAAAOwwHHb6hkAAACEElEQVQ4y6WSy3KcMBBF+f9fSxaJ7VTMADOMeAm9UKuREAhBCuLEXtjxIqe6VNqcUt9uJZRSzjkiUtr3fW8MSinapjEGAIAxDgBKKeesRld2gEYXxRWNRhwTQgipCGOsaRrO+l4MFyJ+5G1RCXLCOZdSMtbXdUPqtmtbUlX3Vg7DkDDKaEuHAQBw9n60znnv/GwMdl2nlDTGaAApZd9TKbgQwiA2HMdxTFYMs/YdBUIY6GFBXAGCMVuM+2ck+74758rylmWZlDJy7gmZ6irM/iNnO+tFDktomiZN07qupZTB2uNxgKBURFyNWaSbxsmgkVIiwOvLC1ezcHoYqrqmbTtwPlu7ex/9HJ3b5jlO02rnMAfvvbXWO/cqbzE660IIMcZ1jdvJ/qexTzKvZpn1hOOq9WLMGpaZn/M04yikVCfW2vdlNAb0cIRBVEpKKebJrqAXKYL38eR3L+/I94re7u31dvyRoqiLohrBGdJBXi5CbOu/Fpak31SdQp6r55Q9p32WicuFf3+oHp/U55m/flFPD1BcMctNlh+XvDA/n4dLhmCChhDjh7NLLjm7k/56o3Uj86LNi0ZIa+2GGHAMVT0ZE/Z9jy972N7mT55TRipW3nmWN007lCXL8oZSuN9ZUdSPTxTgkP8qMca+77uuQ8SkqtWtZISIQS8ajj7hPLVejhqWEOLhviGcrOua7P/BLxI/y20EaxU4AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"prototype &amp; protypal inheritance in JavaScript\"\n        title=\"prototype &amp; protypal inheritance in JavaScript\"\n        src=\"/static/9006996c6eef52e6d097d2f29e97c35e/27c0d/prototype-6.png\"\n        srcset=\"/static/9006996c6eef52e6d097d2f29e97c35e/56d15/prototype-6.png 200w,\n/static/9006996c6eef52e6d097d2f29e97c35e/d9f49/prototype-6.png 400w,\n/static/9006996c6eef52e6d097d2f29e97c35e/27c0d/prototype-6.png 471w\"\n        sizes=\"(max-width: 471px) 100vw, 471px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>We get <del>My full name is Phillip Knightley</del>. Because, even though we had deleted the <del>firstName</del> property, <del>personOne</del> object inherited the <del>firstName</del> property from its prototype (<em>the <del>person</del> object</em>).</p>\n<p>Another important concept to remember is that any changes made to the prototype object will reflect in the child object.</p>\n<p>Let’s assign the <del>firstName</del> property inside the <del>person</del> object (<em>the prototype object</em>) a new value called <del>Pete</del>. This change is propagated to the child (<del>personOne</del>) object .</p>\n<p>Now if we log <del>personOne.fullName()</del> to the console, we get <del>My full name is Pete Knightley</del>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 471px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/3051493d4f381ad69f76c733a819917b/27c0d/prototype-7.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 90.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAIAAADUsmlHAAAACXBIWXMAAA7DAAAOwwHHb6hkAAACE0lEQVQ4y52S6W4bMQyE9/3frwUax97TXmklUdR97xa2E6Roi6ToB0I/BIw4IqcTACshqBQqRbcNAIwx27ZxzpxzAABSKq2V0imGkWpt7DRNILhztpumaRxH8uC6XBljC5Hfz+Tb621Z2eV8JpQyxjjnhJBhXteVzPPUz3Tj0BFCNkIFF1KqEGIIwcUYc/EhSCnXdXXOIaLWmhAiOGOMGaM3sKhtV3KpkJCZYeTztHqjkpQZoHh/PNn3x7Eff9A9bwFg6C/9OASlClnjdYmct+Mz9v0pboe1tu+H0+mVMaYRmzFFYdW6SNm8K6CTTtZaRJRC7LW+dy4pURkwCBC3241TohH3UlqMLaUW455SC6nGmnOOMQbv9/bmqTuOo9YaQng4afs7xz/Q7W3PIgSdEbMQKfhsjWacW+cEgHiglGrtLxPoSikggHPOGJcSCSGIsnqXKM0o99Y+MdLlnCmlhJJnEqZplhKad3FZ0rpW5z61XfcKqZrivDPGWOvuhhFBqa//XGvVShl9z68QDIC35AvwZPTXYu8iPc3iylEnAC8xWFsNOI3B2Op8/WTu3aVXp5f1cl6HgY0j//FyHYYNVUXVtCnLNRpbjuNoH0v8eK3rR3MZtnlhw0j7gVxv0A9kY+Z8vo3jeunR2vYe8Decc8uyKKU6VOH1Qoe7XiIWea8MMgtIAhJi/rXnb3Qx1VbL8V/8BLL3Cz/G2aLVAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"prototype &amp; protypal inheritance in JavaScript\"\n        title=\"prototype &amp; protypal inheritance in JavaScript\"\n        src=\"/static/3051493d4f381ad69f76c733a819917b/27c0d/prototype-7.png\"\n        srcset=\"/static/3051493d4f381ad69f76c733a819917b/56d15/prototype-7.png 200w,\n/static/3051493d4f381ad69f76c733a819917b/d9f49/prototype-7.png 400w,\n/static/3051493d4f381ad69f76c733a819917b/27c0d/prototype-7.png 471w\"\n        sizes=\"(max-width: 471px) 100vw, 471px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Hope you now have a basic understanding of prototypes and prototype-based inheritance in JavaScript.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .synthwave-84 { background-color: #262335; }\n  .synthwave-84 .mtkb { font-weight: bold; }\n  .synthwave-84 .mtki { font-style: italic; }\n  .synthwave-84 .mtk10 { color: #FEDE5D; }\n  .synthwave-84 .mtk1 { color: #FFFFFF; }\n  .synthwave-84 .mtk3 { color: #FF7EDB; }\n  .synthwave-84 .mtk12 { color: #FFFFFFEE; }\n  .synthwave-84 .mtk14 { color: #B6B1B1; }\n  .synthwave-84 .mtk16 { color: #FF8B39; }\n  .synthwave-84 .mtk4 { color: #848BBD; }\n  .synthwave-84 .mtk6 { color: #36F9F6; }\n  .synthwave-84 .mtk9 { color: #FE4450; }\n  .synthwave-84 .mtk8 { color: #72F1B8; }\n  .synthwave-84 .mtk7 { color: #2EE2FA; }\n  .synthwave-84 .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(255, 255, 255, 0.1));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(255, 255, 255, 0.5));\n  }\n</style>","frontmatter":{"title":"A Beginner's Guide to Prototypes & Prototypal Inheritance in JavaScript","date":"2021-05-21"}}},"pageContext":{"slug":"/a-beginners-guide-to-prototypes-and-prototypal-inheritance-in-javascript/","prev":{"fields":{"slug":"/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-5/"},"frontmatter":{"modules":null}},"next":{"fields":{"slug":"/git-cheatsheet/"},"frontmatter":{"modules":null}}}},
    "staticQueryHashes": ["3159585216"]}