<?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: Guddu Kumar</title>
    <description>The latest articles on DEV Community by Guddu Kumar (@omguddu100).</description>
    <link>https://dev.to/omguddu100</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%2F918526%2Ffb02c915-fb01-4f40-b66f-218d9411d444.png</url>
      <title>DEV Community: Guddu Kumar</title>
      <link>https://dev.to/omguddu100</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omguddu100"/>
    <language>en</language>
    <item>
      <title>This keyword in JavaScript</title>
      <dc:creator>Guddu Kumar</dc:creator>
      <pubDate>Wed, 31 Aug 2022 09:47:25 +0000</pubDate>
      <link>https://dev.to/omguddu100/this-keyword-in-javascript-1f5f</link>
      <guid>https://dev.to/omguddu100/this-keyword-in-javascript-1f5f</guid>
      <description>&lt;p&gt;In JavaScript, &lt;u&gt;the this&lt;/u&gt; keyword refers to an &lt;strong&gt;object&lt;/strong&gt;. &lt;br&gt;
Which object depends on how &lt;u&gt;this&lt;/u&gt; is being invoked (used or called). &lt;br&gt;
The &lt;u&gt;this&lt;/u&gt; keyword refers to different objects depending on how it is used: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In an object method, this refers to the object. &lt;/li&gt;
&lt;li&gt;Alone, this refers to the global object. &lt;/li&gt;
&lt;li&gt;In a function, this refers to the global object. &lt;/li&gt;
&lt;li&gt;In a function, in strict mode, this is undefined. &lt;/li&gt;
&lt;li&gt;In an event, this refers to the element that received the event. &lt;/li&gt;
&lt;li&gt;Methods like call(), apply(), and bind() can refer this to any object. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;this in a Method&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When used in an object method, this refers to the object. &lt;/li&gt;
&lt;li&gt;In the example on below, this refers to the person object. &lt;/li&gt;
&lt;li&gt;Because the fullName method is a method of the person object.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 const person = {
    firstName: "John",
    lastName: "Doe",
    id: 5566,
    fullName : function() {
      return this.firstName + " " + this.lastName;
    }
};
console.log(person.fullName())//John Doe

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

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;this Alone&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When used alone, &lt;u&gt;this&lt;/u&gt; refers to the global object. &lt;/li&gt;
&lt;li&gt;Because &lt;u&gt;this&lt;/u&gt; is running in the global scope. &lt;/li&gt;
&lt;li&gt;In a browser window the global object is &lt;em&gt;[object Window]&lt;/em&gt;: &lt;/li&gt;
&lt;li&gt;In &lt;u&gt;strict mode&lt;/u&gt;, when used alone, this also refers to the global object:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; let x=this;
console.log(x)//window
console.log(this)//window
console.log(window==this)//true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;this in a Function (Default)&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In a function, the global object is the default binding for this. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In a browser window the global object is [object Window]: &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In regular function and arrow function this refers to window object.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function a(){
    return this;
  }
  console.log(a())//window

  let b=()=&amp;gt;{
    return this;
  }
  console.log(b())//window
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;this in a Function (Strict)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript strict mode does not allow default binding. &lt;br&gt;
So, when used in a function, in strict mode, this is undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'use strict'
  function a(){
    return this;
  }
  console.log(a())//undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explicit Function Binding&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;call()&lt;/em&gt; and &lt;em&gt;apply()&lt;/em&gt; methods are predefined JavaScript methods. &lt;/p&gt;

&lt;p&gt;They can both be used to call an object method with another object as argument. &lt;/p&gt;

&lt;p&gt;The example below calls person1.fullName with person2 as an argument, this refers to person2, even if fullName is a method of person1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 const person1 = {
    firstName:"Manish",
    lastName:"Kumar",
    fullName: function() {
      return this.firstName + " " + this.lastName;
    }
  }

  const person2 = {
    firstName:"John",
    lastName: "Doe",
  }

console.log(person1.fullName.call(person3))//John Doe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Function Borrowing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With the &lt;em&gt;bind()&lt;/em&gt; method, an object can borrow a method from another object. &lt;/p&gt;

&lt;p&gt;This example creates 2 objects (person and member). &lt;/p&gt;

&lt;p&gt;The member object borrows the fullname method from the person object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const person11 = {
    firstName:"John",
    lastName: "Doe",
    fullName: function () {
      return this.firstName + " " + this.lastName;
    }
  }

  const member = {
    firstName:"Hege",
    lastName: "Nilsen",
  }

  let fullName = person11.fullName.bind(member);
  console.log(fullName())//Hege Nilsen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you like it, please share it.....&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
