<?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: Kalypso Homan</title>
    <description>The latest articles on DEV Community by Kalypso Homan (@catcatmcgee).</description>
    <link>https://dev.to/catcatmcgee</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1042050%2Fd66da332-3629-4dcc-b256-9d1e28190d76.jpeg</url>
      <title>DEV Community: Kalypso Homan</title>
      <link>https://dev.to/catcatmcgee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/catcatmcgee"/>
    <language>en</language>
    <item>
      <title>"This" Keyword in JS</title>
      <dc:creator>Kalypso Homan</dc:creator>
      <pubDate>Thu, 09 Mar 2023 17:39:11 +0000</pubDate>
      <link>https://dev.to/catcatmcgee/this-keyword-in-js-2nkj</link>
      <guid>https://dev.to/catcatmcgee/this-keyword-in-js-2nkj</guid>
      <description>&lt;p&gt;&lt;em&gt;The "this" keyword is a fundamental concept in JavaScript. The value of "this" is dynamically determined at runtime based on how it's called. In this article, we'll explore some key concepts related to the "this" keyword in JavaScript.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Global Object and the "this" Keyword
&lt;/h2&gt;

&lt;p&gt;In JavaScript, the global object is the object that all global variables and functions are defined on. When the "this" keyword is used in the global scope, it refers to the global object. "this" also refers to the global object when it is used in a function that is called in the global scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(this === window); // Output: true

function exampleFunction() {
  console.log(this === window); 
}

exampleFunction(); // Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Implicit Binding
&lt;/h2&gt;

&lt;p&gt;Implicit binding is a common way to determine the value of the "this" keyword in JavaScript. Implicit binding is invoked when the "this" keyword is used in an object's method. In those cases, "this" refers to the object that is immediately to the left of the dot when a function is called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {
  method() {
    console.log(this);
  }
};

obj.method(); // Output: obj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Explicit binding is another way to assign the value of the "this" keyword in JavaScript. It involves using methods such as call(), apply(), or bind() to explicitly set the value of "this" to a particular object. &lt;/p&gt;

&lt;p&gt;The call() and apply() methods are used to invoke a function with a specified "this" value and a set of arguments. They take the "this" value as its first argument, and any additional arguments as subsequent arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var person = {
  firstName: "Johny",
  greeting: function(message) {
    return message + this.firstName + ".";
  }
};

var person2 = {
  firstName: "Sally",
};

var welcome = person.greeting.call(person2, "Hello ");

console.log(welcome); // Output: "Hello Sally."

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

&lt;/div&gt;



&lt;p&gt;The bind() method returns a new function that is bound to a specific "this" value, which means that when the new function is called, the "this" value will be the value that was passed to bind().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var person = {
  firstName: "Johny",
  greeting: function(message) {
    console.log(message + this.firstName + ".");
  }
};

var person2 = {
  firstName: "Sally",
};

var boundFunction = person.greeting.bind(person2, "Hello ");

boundFunction(); // Output: "Hello Sally."

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Constructor Functions
&lt;/h2&gt;

&lt;p&gt;Constructor functions are a way to create new instances of formulaic objects. When a constructor function is called with the "new" keyword, a new object is created and the "this" keyword is bound to that new object. The new object is then returned by the constructor function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function constructorObj(arg) {
  this.prop = arg;
  this.method = function() {
    console.log(this.prop);
  };
}

const exampleObject = new constructorObj('example');
exampleObject.method(); // Output: 'example'

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;The "this" keyword is a fundamental concept in JavaScript programming. The global object, implicit and explicit binding, and constructor functions are concepts that developers should be familiar with when working with the "this" keyword in JavaScript.&lt;/p&gt;

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