<?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: ALEJANDRO</title>
    <description>The latest articles on DEV Community by ALEJANDRO (@alexbcn84).</description>
    <link>https://dev.to/alexbcn84</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%2F241418%2Fe6bb762e-d9c1-40b3-a82b-2db16b29bcab.jpeg</url>
      <title>DEV Community: ALEJANDRO</title>
      <link>https://dev.to/alexbcn84</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexbcn84"/>
    <language>en</language>
    <item>
      <title>UNDERSTANDING THE "THIS" KEYWORD IN JAVASCRIPT</title>
      <dc:creator>ALEJANDRO</dc:creator>
      <pubDate>Wed, 02 Oct 2019 21:27:47 +0000</pubDate>
      <link>https://dev.to/alexbcn84/understanding-the-this-keyword-in-javascript-f0h</link>
      <guid>https://dev.to/alexbcn84/understanding-the-this-keyword-in-javascript-f0h</guid>
      <description>&lt;p&gt;For someone who starts learning JavaScript as their first coding language, understanding the this keyword is one of the big challenges to be encountered. It took me quite a while to fully assimilate the different use cases of this JavaScript concept. It is one of those core aspects such as scope, closures and hoisting that need a lot of drilling and practice until they sink in for good.&lt;/p&gt;

&lt;p&gt;After reading documentation from different sources and following a few tutorials online, I got to understand when and how to use it. Although practice is what made it stick in. &lt;/p&gt;

&lt;p&gt;And now let's get into the meat. The this keyword behaves somehow as an object pointer. It points at different things based on how it is called. Unless otherwise specified, this will always point to its parent object. By default, this parent object is the window object, which contains the DOM document. Window is the global object in the browser. However, it is important to note here, that if we run the same function in the terminal using Node.js, i.e the backend JavaScript run-time environment, the default value of this is global object since we are no longer in the browser.&lt;/p&gt;

&lt;p&gt;In the following lines I will divide this core javascript concept in four different use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Case 1: When used in combination with &lt;code&gt;bind&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt;, and &lt;code&gt;call&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When used with the javascript native methods bind, apply or call, the value of this is set explicitly, and it always behaves the same, giving us full control over what we assign as this. That being said, there are several differences between these three methods. And I will explain them in the lines that follow. But what the three of them have in common is that they change the value of this to something else.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Used with bind&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bind&lt;/code&gt; is a method on functions by default, and so are &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt;. &lt;code&gt;bind&lt;/code&gt; returns a copy of the function it's called on, where &lt;code&gt;this&lt;/code&gt; is set to the first argument passed into &lt;code&gt;bind&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the next section we will tackle &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt;, but for now let’s test how &lt;code&gt;this&lt;/code&gt; works with &lt;code&gt;bind&lt;/code&gt;. First of all, let’s write a simple function that prints out the default value of &lt;code&gt;this&lt;/code&gt;. Go to your favourite browser open the &lt;a href="https://developers.google.com/web/tools/chrome-devtools/open"&gt;developer tools&lt;/a&gt;, and open the console to write the function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;whatIsThis&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="nx"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We are now going to use this function with bind in order to change the default value of this into something of our choice. We will declare a variable, and we will assign to this variable the returned function that results from binding this to the object that we pass as an argument of bind. You can now call the new function. What do you get as an output? What is the new value of this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;explicitlySetTheValueOfThis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;whatIsThis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&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;Barcelona&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;explicitlySetTheValueOfThis&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;bind vs apply and call:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; change the value of &lt;code&gt;this&lt;/code&gt; inside the function, just like &lt;code&gt;bind&lt;/code&gt; does. The big difference though, is that they also run the new function immediately. As we saw, &lt;code&gt;bind&lt;/code&gt; just returns a copy of the function, and then you have to run the new function as a second step.&lt;/p&gt;

&lt;p&gt;Something important to note here is that a function returned from &lt;code&gt;bind&lt;/code&gt; cannot be bound to a different &lt;code&gt;this&lt;/code&gt; value ever again.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;apply vs call:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If the function where we use &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; has no arguments other than the first one -where we pass in the new value of &lt;code&gt;this&lt;/code&gt;, then both methods will behave alike. However, if the function where we use &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; has arguments, &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; will behave differently.&lt;/p&gt;

&lt;p&gt;And here is the difference: when you pass arguments into a function with &lt;code&gt;apply&lt;/code&gt; you need to pass them in an array. So the second argument of &lt;code&gt;apply&lt;/code&gt; is an array whose items are the arguments that we were going to pass into the original function.&lt;/p&gt;

&lt;p&gt;Contrarily to &lt;code&gt;apply&lt;/code&gt;, with &lt;code&gt;call&lt;/code&gt; we don't use an array, we just pass the arguments into the function one after the other, following the first argument, where we had passed in the new value of &lt;code&gt;this&lt;/code&gt; that we wanted to bind with &lt;code&gt;call&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As a result, if we copy and paste the following code into the console, the output will be the same in both cases. The only thing that changes is the way we provide the function with the arguments that it needs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;logThisWithGreeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greeting&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greeting&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="nx"&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="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;logThisWithGreeting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&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;Alex&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="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;logThisWithGreeting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="s1"&gt;Alex&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;hi&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;Alex&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;h2&gt;
  
  
  &lt;strong&gt;Case 2: When &lt;code&gt;this&lt;/code&gt; appears in object methods.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When a function is called as a method - a function inside an object, &lt;code&gt;this&lt;/code&gt; points to that parent object. Let's write some code that represents this scenario. In the same console you previously opened, write now an object which contains a method. All this method should do, like the function we saw in case 1, is to log the value of &lt;code&gt;this&lt;/code&gt;. Then call that object method. What output do you get? You should get the object that, when calling the method, was on the left side of the dot. You will not see the name of the object, but you should get an object that contains the method that you just called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;strangeObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="na"&gt;myMethod&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;strangeObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myMethod&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Case 3: When &lt;code&gt;this&lt;/code&gt;  is used in constructor functions.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When a function is called as a constructor, &lt;code&gt;this&lt;/code&gt; points to the object that the constructor is creating. In case you don't have a clear idea of what a constructor function does, here is a &lt;a href="https://www.youtube.com/watch?v=23AOrSN-wmI"&gt;video&lt;/a&gt; that might enlighten you. But in short, a constructor is a function that returns an object. Factory functions return objects too, but constructor functions are called with the &lt;code&gt;new&lt;/code&gt; keyword and they are capitalised. &lt;a href="https://medium.com/javascript-scene/javascript-factory-functions-vs-constructor-functions-vs-classes-2f22ceddf33e"&gt;Here&lt;/a&gt; is a very good article by Eric Elliot that helped me better understand the difference between constructor and factory functions.&lt;/p&gt;

&lt;p&gt;Let's now test &lt;code&gt;this&lt;/code&gt; in constructor functions. Open your console, and write a constructor function to create objects with one property, the city name. The function will have one parameter, which will take a string argument, the name of a city. Now, you can assign to a variable called &lt;code&gt;myCity&lt;/code&gt; the value returned from running the constructor function with the &lt;code&gt;new&lt;/code&gt; keyword. Don't forget to pass in your city as the argument. Here's an example. What output did you get? console.log the variable to see it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;City&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;var&lt;/span&gt; &lt;span class="nx"&gt;myCity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;City&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Barcelona&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myCity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Case 4: &lt;code&gt;This&lt;/code&gt; and regular functions vs arrow functions&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When we explained &lt;code&gt;bind&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; in the first use case, we wrote the following function to log the value of &lt;code&gt;this&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// this used in regular functions&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;whatIsThis&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="nx"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Regular functions, like the one above, and like the object methods that we saw in case 2, have their own &lt;code&gt;this&lt;/code&gt; keyword, which points to the parent object in scope. If the scope of our function at the time it executes is the global object, &lt;code&gt;this&lt;/code&gt; will refer to the global object, as specified in the introduction to this article.&lt;/p&gt;

&lt;p&gt;However, what if we use an arrow function instead? Arrow functions are tricky because they don't always behave like a regular function, even if we can use them for the same purposes in most cases. One of the things they differ from each other is the &lt;code&gt;this&lt;/code&gt; keyword. Simply, arrow functions don't have their own &lt;code&gt;this&lt;/code&gt; keyword. This means we can still use &lt;code&gt;this&lt;/code&gt; inside an arrow function, but its value will not be the parent object of the function. The value of &lt;code&gt;this&lt;/code&gt; in an arrow function is that one of &lt;code&gt;this&lt;/code&gt; as used outside the scope of the function. &lt;/p&gt;

&lt;p&gt;If we write the example above as an arrow function, the result will be the same, not because &lt;code&gt;this&lt;/code&gt; points to the parent object of the function, but because &lt;code&gt;this&lt;/code&gt; is the &lt;code&gt;window object&lt;/code&gt;. It would be as if we ran &lt;code&gt;console.log(this)&lt;/code&gt; in the global scope.&lt;/p&gt;

&lt;p&gt;However, and here we will see different results, what if we use arrow functions as an object method?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// global scope&lt;/span&gt;

&lt;span class="c1"&gt;// regular function used as method&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myObjectRegular&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="na"&gt;regularMethod&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="nx"&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="p"&gt;},&lt;/span&gt; 
&lt;span class="p"&gt;};&lt;/span&gt; 

&lt;span class="nx"&gt;myObjectRegular&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Expected result:&lt;/span&gt;
&lt;span class="c1"&gt;// {regularMethod: ƒ}&lt;/span&gt;

&lt;span class="c1"&gt;// arrow function used as method&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myObjectArrow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="na"&gt;arrowMethod&lt;/span&gt;&lt;span class="p"&gt;:()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="p"&gt;};&lt;/span&gt; 

&lt;span class="nx"&gt;myObjectArrow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Expected result:&lt;/span&gt;
&lt;span class="c1"&gt;// Window {parent: Window, postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, …}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we run the code above, for the regular method, the value of &lt;code&gt;this&lt;/code&gt; is the parent object of the function. But when running the arrow method, the value of &lt;code&gt;this&lt;/code&gt; is the window object. This proves that arrow functions don't have &lt;code&gt;this&lt;/code&gt; and that when we use &lt;code&gt;this&lt;/code&gt; inside of an arrow function, it points to the value of &lt;code&gt;this&lt;/code&gt; as used outside the function.&lt;/p&gt;

&lt;p&gt;As a final note, I would like recap an important detail that I mentioned a few lines ago but didn't give much focus. That is, the &lt;code&gt;this&lt;/code&gt; keyword gets its value assigned when the function is being executed, not when it's being declared.&lt;/p&gt;

&lt;p&gt;Let’s have a look at an example where we grab a DOM element and attach an event listener to it. Where is &lt;code&gt;this&lt;/code&gt; going to refer to? &lt;/p&gt;

&lt;p&gt;Open the developer tools of your browser, edit the DOM and insert a very simple button with an id attribute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"myButton"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;What is this&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then go to the console tab and write the following code, which basically logs to the console the value of this when clicked:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;logValueOfThis&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`value of this: &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="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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myButton&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logValueOfThis&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now if we close the developer tools and click on the button, I don't see &lt;code&gt;valueOfThis: {logValueOfThis: f}&lt;/code&gt;  as the value of &lt;code&gt;this&lt;/code&gt; but  &lt;code&gt;value of this: [object HTMLButtonElement]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This means the value of &lt;code&gt;this&lt;/code&gt; at the time of execution of the callback function was the &lt;code&gt;button&lt;/code&gt; object. Whenever we set up an event listener, the event handler function will  execute as if it were attached to the element object.&lt;/p&gt;

&lt;p&gt;So what will we do to have &lt;code&gt;this&lt;/code&gt; pointing to &lt;code&gt;myObject&lt;/code&gt;? We will need to rebind our method to &lt;code&gt;myObject&lt;/code&gt; by calling &lt;code&gt;bind&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myButton&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logValueOfThis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;WRAPPING UP&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In this article, I tried to introduce in a simple and yet detailed way one of the pillar concepts of JavaScript. I aimed to engage you by testing the cases that I was presenting. And I have provided some support with links to resources in case you need to extend on some of the topics that are mentioned here. Together with the &lt;code&gt;this&lt;/code&gt; keyword, concepts such as higher order functions, callback functions, functional programming, constructor functions and factory functions are key to any individual who aims to become a professional JavaScript developer. &lt;/p&gt;

&lt;p&gt;On a personal note, I started in programming three years ago, following the decision of a major career change. I am a former translator and language teacher and I had never done any programming before whatsoever. It has not been an easy trip and there are many frustrations that came along the way. But thanks to this experience, I have learnt what things are particularly difficult to understand for someone coming into programming following a non-academic path. If that is your case, and you are taking your first steps, this article was specially written with the aim to help you, even if only in an introductory way.&lt;/p&gt;

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