<?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: var-che</title>
    <description>The latest articles on DEV Community by var-che (@varche).</description>
    <link>https://dev.to/varche</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%2F183711%2F7dd688e2-cccc-46e4-a908-5e0f0dd2795d.png</url>
      <title>DEV Community: var-che</title>
      <link>https://dev.to/varche</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/varche"/>
    <language>en</language>
    <item>
      <title>Let’s Build Prototypal Inheritance in JS</title>
      <dc:creator>var-che</dc:creator>
      <pubDate>Thu, 20 Jun 2019 22:22:54 +0000</pubDate>
      <link>https://dev.to/varche/let-s-build-prototypal-inheritance-in-js-56mm</link>
      <guid>https://dev.to/varche/let-s-build-prototypal-inheritance-in-js-56mm</guid>
      <description>&lt;p&gt;The idea for this post is pretty simple. I want to some extent build and with that, illustrate how prototypes work in Javascript.&lt;/p&gt;

&lt;p&gt;We have to make a statement. Every object has to have a property that we will call &lt;strong&gt;delegator&lt;/strong&gt; that points to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;another object, or
&lt;/li&gt;
&lt;li&gt;points to null&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let us quickly define the search-property algorithm, and don’t worry, it is straightforward.&lt;br&gt;&lt;br&gt;
We search for a property in some initial object. If nothing is found and a delegator is an object, perform the same property search in that delegator object. If delegator property is pointing to null, return some generic error like “&lt;em&gt;nothing is found&lt;/em&gt;”.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var first = {
 a: 221,
 delegator: null
}
var second = {
 b: ‘stringy property’,
 delegator: null
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have created two objects with some of their personal properties, and one property name in common. The &lt;strong&gt;delegator&lt;/strong&gt; one. And for now, both of them are pointing to null. By doing this, we have fulfilled our first condition/statement, and that is that every object has to have a &lt;strong&gt;delegator&lt;/strong&gt; property. Great so far. Now, we need to focus on the searching-properties algorithm.&lt;/p&gt;

&lt;p&gt;To look up for the properties in an object, we can use the&lt;code&gt;for&lt;/code&gt; loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for( property in first ){
 if(property === 'a'){
   console.log('I found it') // `I found it`
   return
  } // else there is nothing found
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case you don’t know what this is doing, it is looking for the properties in the object called&lt;code&gt;first&lt;/code&gt; for the property &lt;code&gt;a&lt;/code&gt; . Before I go any further, I would have to refactor this code in a function, since I will be using it (hopefully) many times in my program. We have two variables in it: the name of the object(&lt;em&gt;obj&lt;/em&gt;) and the name of the property(&lt;em&gt;property_name&lt;/em&gt;), so those two will be my arguments in the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function searchProperty(obj, property_name) {
  for( property in obj ){
    if(property === property_name){
      console.log('I found it')
      return
    } // else there is nothing found
  }
}
searchProperty(first, 'a') // 'I found it'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So far, we have a function that performs searching only in one object, but we have said in our algorithm that we need to perform this search recursively on object’s &lt;strong&gt;delegators&lt;/strong&gt; until we find the property &lt;strong&gt;or&lt;/strong&gt; we hit a &lt;strong&gt;delegator&lt;/strong&gt; that points to null.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function searchProperty(obj, property_name) {
 if(obj === null) {
   console.log('We have reached the end of chain. Nothing found.')
    return
  }
  for( property in obj ){
    if(property === property_name){
      console.log('I found it')
      return
    }
  }
  searchProperty(obj.delegator, property_name)
}

searchProperty(first, 'a') // 'I found it'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the first line, we have handled the case when the delegator points to null. All we return is a log that says&lt;code&gt;We have reached the end of chain. Nothing found.&lt;/code&gt; and then we exit the function. Nothing we can do in it anymore, so we return.&lt;br&gt;&lt;br&gt;
After the &lt;code&gt;for&lt;/code&gt; loop, and that is the case if no property is found in the starting object, I would call that search function again with the same &lt;code&gt;property_name&lt;/code&gt; argument, but with a different object to begin the search on. &lt;br&gt;&lt;br&gt;
By doing this, we are searching the properties on delegator objects until we hit the delegator that points to null or we actually get a property that we are searching.&lt;/p&gt;



&lt;p&gt;In this short section, I would like to explore and test our function above and try to walk through the code and guess what the outcome would be.&lt;/p&gt;

&lt;p&gt;Example 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var first = {
 a: 221,
 delegator: null
}
var second = {
 b: 'stringy property',
 delegator: null
}
 ...
searchProperty(second, 'a')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we are searching property &lt;code&gt;a&lt;/code&gt; in the &lt;code&gt;second&lt;/code&gt; object, and since it is not found in that object itself (for loop), we are calling &lt;code&gt;searchProperty(second.delegator, ‘a’)&lt;/code&gt; . As you can see, the &lt;code&gt;delegator&lt;/code&gt; property here points to null, hence returning the “end of the chain” error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1pqiu8bbpixarimkturj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1pqiu8bbpixarimkturj.gif" width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am drawing the objects with head, body, and bottom. On the body, there are hands that point to some values to emulate &lt;em&gt;key-value&lt;/em&gt; pair. So, we are performing the value resolving function for &lt;code&gt;a&lt;/code&gt; in the &lt;code&gt;second&lt;/code&gt; object and since it is not found, then the second function is called, and as an argument, we are resolving the &lt;strong&gt;delegator&lt;/strong&gt; property. It is pointing to a null value and the “&lt;em&gt;error&lt;/em&gt;” was printed.&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 plaintext"&gt;&lt;code&gt;var first = {
 a: 221,
 delegator: null
}
var second = {
 b: 'stringy property',
 delegator: first
}
 ...
searchProperty(second, 'a')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In here, I start searching &lt;code&gt;a&lt;/code&gt; property in the &lt;code&gt;second&lt;/code&gt; object. I haven’t found it in there so I am invoking &lt;code&gt;searchProperty(second.delegator, 'a')&lt;/code&gt; which will result in &lt;code&gt;searchProperty(first, 'a')&lt;/code&gt; , performing the search in &lt;code&gt;first&lt;/code&gt; object looking for &lt;code&gt;a&lt;/code&gt; property. Sure enough, it is found there.&lt;/p&gt;

&lt;p&gt;Example 3&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var first = {
 be: 210021,
 ce: 001,
 __delegator__ : null
}

var second = {
 ey: "lol",
 some: 001,
 __delegator__ : first
}

var third = {
 prop: 'null',
 tup: 21,
 __delegator__ : first
}

searchProperty(third, 'be') // I found it
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Quick obvious note. I have changed the &lt;strong&gt;delegator&lt;/strong&gt; key name into &lt;strong&gt;__delegator__&lt;/strong&gt; because of some chance that user picks that name and our object or a null value will be changed by the user. Be sure to change it in the function body: &lt;code&gt;searchProperty(obj. __delegator__ , property_name).&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz2kzzs4n986c8bz1cckr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz2kzzs4n986c8bz1cckr.gif" width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A quick note regarding animation. Every object will be sitting on its &lt;strong&gt;__delegator__&lt;/strong&gt; rather point to it from the body. The reason for this is an organized view of the system.&lt;/p&gt;

&lt;p&gt;In this case, both &lt;code&gt;second&lt;/code&gt; and &lt;code&gt;third&lt;/code&gt; object have a &lt;strong&gt;delegator&lt;/strong&gt; object &lt;code&gt;first&lt;/code&gt; . Both of them are sitting on the &lt;code&gt;first&lt;/code&gt; object, and he is sitting on top on null. The search was started on &lt;code&gt;third&lt;/code&gt; object and since it is not found, a new search is started on the &lt;code&gt;first&lt;/code&gt; object where the property &lt;code&gt;be&lt;/code&gt; is found. The same result will be achieved when we call this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;searchProperty(second, 'be') // I found it
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The search starts on the &lt;code&gt;second&lt;/code&gt;object, the property is not found, the search has been performed on its &lt;strong&gt;delegator&lt;/strong&gt; , and it is found there.&lt;/p&gt;

&lt;p&gt;In the end, objects don’t inherit anything. It’s just the search that is being continued to other objects.&lt;/p&gt;




&lt;p&gt;Depending on the reception of this post, I will make a follow up to it. While the core mechanic of delegation is demonstrated, I would like to spend more time talking about the topics related to this. I have prepared two more posts on this topic so far and have an idea for one more.&lt;/p&gt;

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