<?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: Sabali </title>
    <description>The latest articles on DEV Community by Sabali  (@sabz).</description>
    <link>https://dev.to/sabz</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%2F440141%2Fb8f6a744-e3e0-43a8-86a3-6fae7cfdb3c6.jpg</url>
      <title>DEV Community: Sabali </title>
      <link>https://dev.to/sabz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sabz"/>
    <language>en</language>
    <item>
      <title>Understanding "this" in javascript</title>
      <dc:creator>Sabali </dc:creator>
      <pubDate>Sun, 16 Jan 2022 18:15:23 +0000</pubDate>
      <link>https://dev.to/sabz/understanding-this-in-javascript-1pjl</link>
      <guid>https://dev.to/sabz/understanding-this-in-javascript-1pjl</guid>
      <description>&lt;p&gt;Simply put "this" in javascript refers to the object itself, for 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 myProfile = {
name:"Sabali",
sayHi:function(){
 console.log("Hi there, " + this.name)
  }
}

myProfile.sayHi() // "Hi there, Sabali"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Javascript is notoriously known for performing behaviors not expected by the developer . One of those unexpected behaviors comes from using the "this" key word by calling it through a reference. Here's a demonstration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let greeting = myProfile.sayHi()
greeting() // "Hi there, undefined"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is undefined reason being when calling a method through a reference, the method loses knowledge of what the original "this" was, in this case this becomes a global object.&lt;br&gt;
In order to use this in an a reference we can employ the bind method.&lt;/p&gt;

&lt;p&gt;Calling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myProfile.greeting.bind(myProfile)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a new function where this is bound to point to Sabali, independent of where and how the method is being called.&lt;/p&gt;

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