<?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: Brit Hemming</title>
    <description>The latest articles on DEV Community by Brit Hemming (@brityhemming).</description>
    <link>https://dev.to/brityhemming</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%2F239592%2Ffb97f24d-0183-4561-a7e0-7e0aa108e429.jpg</url>
      <title>DEV Community: Brit Hemming</title>
      <link>https://dev.to/brityhemming</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brityhemming"/>
    <language>en</language>
    <item>
      <title>What the beep is 'this' in JavaScript</title>
      <dc:creator>Brit Hemming</dc:creator>
      <pubDate>Fri, 07 May 2021 18:23:15 +0000</pubDate>
      <link>https://dev.to/brityhemming/what-the-beep-is-this-in-javascript-483o</link>
      <guid>https://dev.to/brityhemming/what-the-beep-is-this-in-javascript-483o</guid>
      <description>&lt;p&gt;&lt;a href="https://youtu.be/6VvGJoSTaqM"&gt;Watch on YouTube&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/BritHemming/pen/gOmbzbL?editors=0012"&gt;link to codepen if you want to follow along&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The 'this' keyword in JavaScript can be a confusing topic for a lot of new developers. In fact, it's a question I happened to get wrong in a job interview and as a result I've put some effort into studying and understanding it (don't want to make the same mistake twice). This blog post will go through the 4 principles of 'this' and how each one is applied in JavaScript. &lt;/p&gt;

&lt;p&gt;Let's start by talking about what 'this' means in english. If I were to tell you "Hey! Look at this" - what is this? You really have no idea what 'this' refers to unless I give you some context. If I pick up an object and point to it and say "look at this" you will quickly be able to understand that 'this' refers to the object in which I am pointing to. It's the same in JavaScript. If we give this no context at all it will return the window to us, the global object in node and undefined in strict mode. This brings us to our first principle of 'this' &lt;/p&gt;

&lt;h2&gt;
  
  
  1. Window Binding
&lt;/h2&gt;

&lt;p&gt;Window binding is not something we aim to use. It's what happens when we do not give context for the 'this' keyword. If we don't tell JavaScript what 'this' is it will return the window to us, the global object in node or undefined in strict mode. &lt;br&gt;
Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sound(){
console.log(this.bark);
}

sound() // we will get back the window in the console
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This brings us to our next principle: &lt;/p&gt;

&lt;h2&gt;
  
  
  2. Implicit binding
&lt;/h2&gt;

&lt;p&gt;Implicit binding is probably the most common principle of 'this', it apply to objects with methods and it says when the function is invoked, look to the left of the dot. That's what 'this' refers to. &lt;br&gt;
Let's see an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ada = {
name: 'Ada',
breed: 'Bali dog',
bark: 'woof woof'
sound: function(){
    console.log(this.bark);
  }
}

ada.sound(); // invoking the function. This bark refers to ada's bark because ada is the left of the dot when the function is invoked

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

&lt;/div&gt;



&lt;p&gt;A good way to remember implicit binding is that it's IMPLIED that whatever is to the left of the dot when the function is invoked that's what 'this' will refer to. &lt;/p&gt;

&lt;h2&gt;
  
  
  3. Explicit Binding
&lt;/h2&gt;

&lt;p&gt;With explicit binding we explicitly pass in as an argument what we want 'this' to refer to. We do that using &lt;code&gt;.call(), .apply(), or .bind()&lt;/code&gt;. There are some differences with how we use these. &lt;br&gt;
&lt;code&gt;.call()&lt;/code&gt; - will immediately invoke the function, with .call you pass in the arguments 1 by 1&lt;br&gt;
&lt;code&gt;.apply()&lt;/code&gt; - will immediately invoke the function, with .apply you would pass in the arguments as an array&lt;br&gt;
&lt;code&gt;.bind()&lt;/code&gt; - you will pass in your arguments 1 by 1 but it does not immediately invoke the function, instead it returns a brand new function that can be invoked later. &lt;br&gt;
Let's see an example of &lt;code&gt;.call&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sound(){
console.log(this.bark);
}

const ada = {
name: 'Ada',
breed: 'Bali Dog',
bark: 'woof woof'
}

sound.call(ada); // invoking the function here and binding this bark to Ada's bark - we will get back woof woof in the console.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see an example of &lt;code&gt;.bind&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sound(){
console.log(this.bark);
}

const ada = {
name: 'Ada',
breed: 'Bali Dog',
bark: 'woof woof'
}

const goodDog = sound.bind(ada); // creating a new function called goodDog that can be invoked later

goodDog(); // we will get back bark bark in the console
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. New Binding
&lt;/h2&gt;

&lt;p&gt;New binding is used with constructor functions. It says that when a function is invoked as a constructor function using the 'new' keyword 'this' points to the newly created object. At this point you may be asking "what is a constructor function?" Great question! A constructor function constructs other objects, that's it's whole life's purpose. Some things you may notice about a constructor function is that it has a capitalized function name, there is an assignment of the 'this' keyword and it may be missing a return statement. &lt;br&gt;
Let's see an Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Pet(name, breed, bark){
this.name = name;
this.breed = breed;
this.bark = bark;
}
Pet.prototype.sound = function(){ //here we are using the prototype keyword to abstract out the sound method so that we can pass it across newly created objects without if affecting memory
  console.log(this.bark);
}

const ada = new Pet('Ada', 'Bali Dog', 'woof woof'); // creating my new object - this.name will be 'Ada' this.breed will be 'Bali Dog' and this.bark will be 'woof woof' - 'this' is pointing to my newly created object which is ada. 

ada.sound()// will log 'woof woof'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One final call out, do not use arrow functions inside of object methods. Arrow functions don't bind this 'this' keyword, they pass it through. This is not a bug, it's feature of arrow functions. For more on that Wes Bos wrote a really great post called Arrow Function No No's &lt;a href="https://wesbos.com/arrow-function-no-no"&gt;linked here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this was helpful for you! Let me know in the comments &amp;lt;3 &lt;/p&gt;

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