<?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: Prateek Gogia</title>
    <description>The latest articles on DEV Community by Prateek Gogia (@prateek_gogia).</description>
    <link>https://dev.to/prateek_gogia</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%2F102251%2F8a2b12b6-391d-4fab-8aa0-d1eee14fccdf.jpg</url>
      <title>DEV Community: Prateek Gogia</title>
      <link>https://dev.to/prateek_gogia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prateek_gogia"/>
    <language>en</language>
    <item>
      <title>this and bind() in Javascript</title>
      <dc:creator>Prateek Gogia</dc:creator>
      <pubDate>Mon, 17 Jun 2019 10:00:35 +0000</pubDate>
      <link>https://dev.to/prateek_gogia/this-and-bind-in-javascript-7ff</link>
      <guid>https://dev.to/prateek_gogia/this-and-bind-in-javascript-7ff</guid>
      <description>&lt;p&gt;*** Previously published at &lt;a href="https://reeversedev.com/this-and-bind-in-javascript&amp;lt;br&amp;gt;%0A"&gt;my website&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Umw6v1I4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.jwplayer.com/thumbs/trS42zpd-720.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Umw6v1I4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.jwplayer.com/thumbs/trS42zpd-720.jpg" id="responsive" alt="this-and-bind-in-javascript" width="700"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"When dealing with JavaScript, things can get really confusing specially when you have to deal with Object Oriented JavaScript (OOP based). Although, in Object Oriented JavaScript, everything is in a form of functions and objects which is much less complex to understand the language (Really?)".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What is &lt;code&gt;this&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;The keyword &lt;code&gt;this&lt;/code&gt; in Javascript is being used exactly like the word 'this' in English Language which means that whenever we are using &lt;code&gt;this&lt;/code&gt; somewhere, we are talking about some context.&lt;/p&gt;

&lt;p&gt;Let's dig even more by taking reference of (I'll go old school here), the times when there were kingdoms in the whole world and everyone wants to rule each other. A typical conversation at the time of war, would be something like:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;King&lt;/strong&gt;: &lt;em&gt;"We need to capture this peak of the mountain (pointing his finger on the map), before 05:00 hrs in order to control our situation better"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Army Chief&lt;/strong&gt;: &lt;em&gt;"I'm sorry to be so dumb, my king, but which peak exactly?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;King&lt;/strong&gt;: &lt;em&gt;"This peak. (again pointing his finger on the map)"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Army Chief&lt;/strong&gt;: &lt;em&gt;"But boss, without even knowing any proper context for the peak which you are you talking about, how am I supposed to command my army to take action?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;King&lt;/strong&gt;: &lt;em&gt;"(After realising about the situation that he's a literal dumb) We need to capture this peak(now taking finger of army chief and pointing it on the point of map) of Mountain XYZ"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Army Chief&lt;/strong&gt;: &lt;em&gt;"I'll command the army right away"&lt;/em&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img src="http://clipart-library.com/images/piozGxaiE.jpg" width="200"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the above conversation, things got really confusing between two people because both were talking in their own individual contexts which further created confusion.&lt;/p&gt;

&lt;p&gt;Similarly, in Javascript as well, things get really confusing when we deal with &lt;strong&gt;objects&lt;/strong&gt;, &lt;strong&gt;functions&lt;/strong&gt; and &lt;code&gt;this&lt;/code&gt; because we often make a mistake in context.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stuff about Javascript
&lt;/h3&gt;

&lt;p&gt;Here's a thing a about Javascript, it's not only object oriented programming language but also a functional programming language which makes things much easier for a developer who is writing code in it. Higher order functions like map(), reduce() and filter() do reduce the pain of manipulating the code with loops and finally the calculation related to result.&lt;/p&gt;

&lt;p&gt;As a matter of fact, Javascript as a whole is more and more about Objects and Functions. No matter whatever we wanna build with it, we need to make use of Objects and Functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;this&lt;/code&gt; in Javascript
&lt;/h3&gt;

&lt;p&gt;For Object Oriented Programming languages, &lt;code&gt;this&lt;/code&gt; keyword is one of the most important elements of the language, without it, the language is incomplete.&lt;/p&gt;

&lt;p&gt;Let's understand better with some code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = {
    name: 'HODOR',
    dialogue: 'HODOR!',
    talk: function() {
        console.log(this.dialogue)
    }
}

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



&lt;p&gt;We have an object named as 'person' which has name, dialogue and talk property. Now let's call it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;person.talk() // "HODOR!"

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



&lt;p&gt;That's what was expected. Right? Now, let's call it in a different fashion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let talkFunction = person.talk
talkFunction() // undefined

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



&lt;p&gt;Yikes! This is what is happening over here.&lt;/p&gt;

&lt;p&gt;When we are declaring variable &lt;em&gt;talkFunction&lt;/em&gt;, it's no longer a method but rather a function. But again, why does it give &lt;code&gt;undefined&lt;/code&gt;? It's because,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;this&lt;/code&gt; keyword refers to the context where it is &lt;strong&gt;called&lt;/strong&gt; and NOT where it is &lt;strong&gt;assigned&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  So what do we do to use that talkFunction?
&lt;/h4&gt;

&lt;p&gt;Here, &lt;code&gt;bind()&lt;/code&gt; comes in action. A &lt;code&gt;bind()&lt;/code&gt; is responsible for attaching an object to a function by which it has been called.&lt;/p&gt;

&lt;p&gt;Let's take a look at the code again,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = {
    name: 'Jon Snow',
    dialogue: 'We the North!',
    talk: function() {
        console.log(this.dialogue)
    }
}

let talkFunction = person.talk
let bindedFunction = talkFunction.bind(person)
bindedFunction() // "We the North!"

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



&lt;p&gt;Here, &lt;code&gt;bind()&lt;/code&gt; attaches the 'person' object to &lt;em&gt;talkFunction&lt;/em&gt; and therefore, talk function is able to get &lt;code&gt;this.dialogue&lt;/code&gt; and hence prints the dialogue, &lt;strong&gt;"We the North!"&lt;/strong&gt;&lt;/p&gt;

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