<?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>Forem: VIJAY KUMAR REDDY BOLLAVARAM</title>
    <description>The latest articles on Forem by VIJAY KUMAR REDDY BOLLAVARAM (@vijaykumarbollavaram).</description>
    <link>https://forem.com/vijaykumarbollavaram</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%2F145931%2Fae9ab759-95ca-4709-8d63-86f4f15b9246.png</url>
      <title>Forem: VIJAY KUMAR REDDY BOLLAVARAM</title>
      <link>https://forem.com/vijaykumarbollavaram</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vijaykumarbollavaram"/>
    <language>en</language>
    <item>
      <title>this keyword in javascript</title>
      <dc:creator>VIJAY KUMAR REDDY BOLLAVARAM</dc:creator>
      <pubDate>Sun, 12 Jan 2025 06:15:04 +0000</pubDate>
      <link>https://forem.com/vijaykumarbollavaram/this-keyword-in-javascript-4i2a</link>
      <guid>https://forem.com/vijaykumarbollavaram/this-keyword-in-javascript-4i2a</guid>
      <description>&lt;p&gt;One of the most confused keyword in JavaScript is the &lt;code&gt;this&lt;/code&gt; keyword. It's a special identifier keyword that's automatically defined in the scope of every function, but what exactly it refers to confuse even seasoned JavaScript developers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The this keyword refers to the context where a piece of code, such as a function's body, is supposed to run. Most typically, it is used in object methods, where this refers to the object that the method is attached to, thus allowing the same method to be reused on different objects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The value of this can be identified where the function is executed not where the function is declared&lt;/p&gt;

&lt;p&gt;We will examine different rules to identify the this in javascript&lt;/p&gt;

&lt;h2&gt;
  
  
  Default binding
&lt;/h2&gt;

&lt;p&gt;The default rule we will apply most common case of function calls: standalone function execution. Think of this this rule as the default catch-all rule when none of the other rules apply.&lt;/p&gt;

&lt;p&gt;In standalone functions call &lt;code&gt;this&lt;/code&gt; value will be globalObject (in browser env it is window object, in node env it will be global)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function bar() {
    console.log(this) // this will be global object (window)
}
bar()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But......&lt;/p&gt;

&lt;p&gt;the value of this can be different how the code is running in strict mode or non strict mode&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If When function invoked as standalone function this typically refer to global object in non strict mode and &lt;code&gt;undefined&lt;/code&gt; in strict mode&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;/div&gt;



&lt;p&gt;Subtle but important, the global object is only eligible for the default binding if the contents of bar() are not running in strict mode;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function bar() {
    console.log(this) // global object (window)
}
(function() {
    "use strict"
    bar()
})()

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

&lt;/div&gt;



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

&lt;p&gt;When a regular function is invoked as a method of an object (obj.method()), this points to that object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function bar() {
    console.log(this)
}
const obj = {
    name: "javascript",
    bar,
}
obj.foo() // this here is object owing the function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Firstly, notice the manner in which bar() is declared and then later added as a reference property onto obj. Regardless of whether bar() is initially declared on obj, or is added as a reference later (as this snippet shows), in neither case is the function really "owned" or "contained" by the obj object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implicit lost:&lt;/strong&gt;&lt;br&gt;
When using callbacks, implicitly bound function loses that binding, which usually means it falls back to the default binding, of either the global object or undefined, depending on strict mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function bar() {
    setTimeout(function() {
        console.log(this) // this will be global object
    }, 1000);
}
const obj = {
    name: "javascript",
    bar
}
obj.bar() // this will be global object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function bar() {
    console.log(this)
}
const obj = {
    name: "javascript",
    bar
}
const a = obj.bar
a() // this will be global object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explicit binding
&lt;/h2&gt;

&lt;p&gt;With implicit binding as we just saw, we had to mutate the object in question to include a reference on itself to the function, and use this property function reference to indirectly (implicitly) bind this to the object.&lt;/p&gt;

&lt;p&gt;But, what if you want to force a function call to use a particular object for the this binding, without putting a property function reference on the object?&lt;/p&gt;

&lt;p&gt;Yes its possible, javascript provide many methods like .map, .filter for array we have few methods in function. These are &lt;code&gt;apply&lt;/code&gt; , &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;bind&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is the syntax for these methods&lt;br&gt;
&lt;strong&gt;call&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn.call(context, arg1, arg2, arg3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;apply&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn.apply(context, [arg1, arg2, arg3])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Subtle different between apply and call. The syntax is same but we pass arguments as array in apply method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function bar() {
    console.log( this.a );
}
var obj = {
    a: 2
};
bar.call( obj ); // 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Invoking bar with explicit binding by bar.call(..) allows us to force its this to be obj.&lt;/p&gt;

&lt;h2&gt;
  
  
  new Binding
&lt;/h2&gt;

&lt;p&gt;When a function is used as a constructor (with the new keyword), its this is bound to the new object being constructed, no matter which object the constructor function is accessed on. The value of this becomes the value of the new expression unless the constructor returns another non–primitive value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function C() {
  this.a = 37;
}

let o = new C();
console.log(o.a); // 37

function C2() {
  this.a = 37;
  return { a: 38 };
}

o = new C2();
console.log(o.a);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  this in arrow function
&lt;/h2&gt;

&lt;p&gt;Arrow-functions adopt the this binding from the enclosing (function or global) scope.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj1 = {
    name: "Vijay", 
    getName: function foo() {
        const a = () =&amp;gt; {
            console.log(this) // this will be its enclosing scope this means foo this
        }
        a()
    }
}

obj1.getName()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a = () =&amp;gt; {
    console.log(this) // this will be global object. 
}
const obj1 = {
    name: "Vijay", 
    getName: function() {
        a()
    }
}

obj1.getName()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you check above example, this is window object not inherit from foo. As I said earlier for arrow function this will inherit from lexical scope when they are declaring&lt;/p&gt;

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