<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kashif Roshen on Medium]]></title>
        <description><![CDATA[Stories by Kashif Roshen on Medium]]></description>
        <link>https://medium.com/@kashifroshen?source=rss-f2305e8f5416------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*OlSHATjcl6Z8fZgL.</url>
            <title>Stories by Kashif Roshen on Medium</title>
            <link>https://medium.com/@kashifroshen?source=rss-f2305e8f5416------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 01:10:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kashifroshen/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Prototypal Inheritance in JavaScript]]></title>
            <link>https://levelup.gitconnected.com/prototypal-inheritance-in-javascript-c03725a0d789?source=rss-f2305e8f5416------2</link>
            <guid isPermaLink="false">https://medium.com/p/c03725a0d789</guid>
            <category><![CDATA[prototypal-inheritance]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[inheritance]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Kashif Roshen]]></dc:creator>
            <pubDate>Sun, 19 Apr 2020 13:03:15 GMT</pubDate>
            <atom:updated>2020-05-18T12:19:13.594Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/853/1*q60IszqHud-LeY3W5XBGsw.png" /></figure><h3>Introduction</h3><p>JavaScript is the weird kid on the block. It is pretty quirky, but the way I see it, these quirks make JavaScript the most flexible language out there. However many developers are not familiar with these specialties and that obstructs them from writing good code.</p><p>This article is the first in a series where I attempt to throw some light on the fundamental concepts of JavaScript that most developers aren’t familiar with.</p><p>The major concept I am going to focus on in this article is <strong>inheritance</strong>. <strong>Inheritance</strong> could very well be the most significant but misunderstood concept in JavaScript, especially by developers with a background in class-based languages.</p><p>Though JavaScript introduced classes in <strong>ES2015</strong>, it does not rely on them for implementing <strong>inheritance</strong>, rather it is merely syntactic sugar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nnCJdECrpSGZ9sfCEOEgFA.png" /></figure><p>JavaScript classes are glorified <strong>constructor functions,</strong> and under the hood, they still use <strong>prototypal inheritance</strong>.</p><h3>What is prototypal inheritance?</h3><p>In JavaScript, we can create an object without defining its class. Objects can also directly inherit properties from other objects. This feature sets JavaScript apart from class-based languages, where classes inherit from other classes.</p><p>Before we further dig into how <strong>prototypal inheritance</strong> works, let&#39;s discuss how objects behave in JavaScript.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4c0453d8697eca48dfdc08cf3625754/href">https://medium.com/media/c4c0453d8697eca48dfdc08cf3625754/href</a></iframe><p>Objects in JavaScript are key-value pairs. The simplest way to create an object is using curly braces (<strong>literal form</strong>), with properties and methods defined inside them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33f954658516965b5df5edaa6b100cb5/href">https://medium.com/media/33f954658516965b5df5edaa6b100cb5/href</a></iframe><p>You can look up properties in an object using <strong>dot nation</strong>. You can also use <strong>dot nation</strong> to add or modify properties.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d531bd2d1d3909ad143b85dea87b8f5/href">https://medium.com/media/8d531bd2d1d3909ad143b85dea87b8f5/href</a></iframe><p>If we look up a property that exists in an object, we get the value assigned to it. If we look up a property that <em>doesn’t exist</em> in an object, we get undefined as the value. These two rules hold <em>unless</em> the property exists in the <strong>prototype chain</strong>.</p><h4>What is the <strong>prototype chain</strong>?</h4><p>Each object holds a link to another object, called its <strong>prototype</strong>. That object also has a <strong>prototype</strong>, and this continues until we reach an object in which the <strong>prototype</strong> is null. The resulting series of inter-connected objects is called the <strong>prototype chain</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ISblNaDIenezoiVJXvbQyQ.png" /></figure><p>We can investigate this relationship using Google DevTools. As you can see, I have created an empty object, but it has a single property named __proto__ with an object assigned to it. __proto__ is the key that holds an object’s <strong>prototype</strong>(also called the <strong>delegate prototype</strong>). From this point onwards, I will refer to this property as <strong>[[prototype]] </strong>since that’s the standard.</p><p>When we try to access a property in an object, the property would be looked for in the object itself, then in its <strong>prototype</strong> and so on, till either a property with a matching name is found, <em>or </em>the end of the <strong>Prototype Chain</strong> is reached. Once a matching name is found, the lookup is terminated.</p><p>This behaviour is evident in the example given below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/909e0ae1a23b7d6daaf22ce775aac3d7/href">https://medium.com/media/909e0ae1a23b7d6daaf22ce775aac3d7/href</a></iframe><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf"><strong>Object.setPrototypeOf()</strong></a><strong> </strong>sets an object (or null) as the <strong>[[prototype]] </strong>of a given object.</p><p>Since book <em>doesn’t have</em> a property named title, the lookup is delegated to the <strong>[[prototype]] </strong>of<strong> </strong>book, publication. publication has a property named title so the value assigned to it would be printed to the console.</p><p>Since book <em>has</em> a property named publishedYear, the lookup would not be delegated to publication and the value assigned to book.publishedYear would be printed to the console.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a0f386bc67a80f62c0e91e69a4e3442/href">https://medium.com/media/6a0f386bc67a80f62c0e91e69a4e3442/href</a></iframe><p>Above, you can see the same scenario implemented using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create"><strong>Object.create()</strong></a><em>. </em><strong>Object.create() </strong>accepts an object (or null) and sets it as the <strong>[[prototype]] </strong>of a brand new object.</p><p><strong>Object.setPrototypeOf() </strong>and <strong>Object.Create() </strong>come in handy when we need to utilize <strong>prototypal inheritance</strong>. But both these methods are relatively new to JavaScript. The age-old mechanism of implementing <strong>prototypal inheritance</strong> is using c<strong>onstructor functions</strong>.</p><h4>What are constructor functions?</h4><p><strong>Constructor functions</strong> are regular functions that initialize an object when they are executed with the new operator. Executing a <strong>constructor function</strong> with the new keyword is termed a <strong>constructor call</strong>.</p><p>There’s one thing you should know about functions before we continue.</p><p>All JavaScript functions (except arrow functions) have an inbuilt property named prototype. <strong>Do not confuse this property with </strong><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#Function_prototype_object"><strong>[[prototype]]</strong></a><strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*62ztxn5Hi6xcQe9VobhOVg.png" /></figure><p>The prototype property of a function, <em>unless mutated</em>, has a single own property named constructor, which points to the memory location of the function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9585278caab1bbf509a55bb53138fb34/href">https://medium.com/media/9585278caab1bbf509a55bb53138fb34/href</a></iframe><p>A c<strong>onstructor call</strong> results in three things.</p><ol><li>A brand new object is created. It could either be a newly constructed object or an alternative object returned by the function.</li><li>The created object is set as the this context for that particular call.</li><li>The prototype property of the function is set as the <strong>[[prototype]]</strong> of the new object.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b0935b931dcde44b32a087c2ed0f615/href">https://medium.com/media/4b0935b931dcde44b32a087c2ed0f615/href</a></iframe><p>The above example combines all the concepts we have discussed to simulate a classic case of <strong>inheritance</strong>.</p><p>To provide access to the properties and methods of Publication, to new instances of Book, I am passing the this context inside Book to Publication, using <strong>Function.prototype.call()</strong>. Since this context inside Book refers to the new instance, the properties and methods of Publication are added to it.</p><p>Since instances of Book should also be able to access custom methods residing in the <strong>[[prototype]]</strong> of Publication, I am using <strong>object.setPrototypeOf() </strong>to<strong> </strong>link the <strong>[[prototype]]</strong>’s of vehicle and book.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aV9ZbrUVjR4kQv5S_2iJ5g.png" /></figure><p>We can use the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof"><strong>instanceOf</strong></a><strong> </strong>operator to verify whether the prototype property of a<strong> constructor function </strong>appears anywhere along the <strong>Prototype Chain</strong>.</p><h3>Recap</h3><p>In JavaScript, objects can inherit from other objects. Every Object has a single ancestor object, and this results in a chain of interlinked objects. Objects in this chain can access properties and methods of objects that come before them. <strong>Object.setPrototypeOf()</strong>, <strong>Object.create() </strong>and <strong>constructor functions </strong>are three ways using which you can inter-link objects to achieve <strong>inheritance</strong>.</p><h3>Up Next</h3><ul><li>Classes and how they work under the hood</li><li><strong>Concatenative inheritance</strong> (<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign"><strong>Object.assign()</strong></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax"><strong>Spread syntax</strong></a> etc.)</li><li><strong>Functional inheritance</strong> (<strong>Factory functions</strong>)</li></ul><p>See you soon, and stay safe! 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c03725a0d789" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/prototypal-inheritance-in-javascript-c03725a0d789">Prototypal Inheritance in JavaScript</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>