<?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: vishwa@chauhan</title>
    <description>The latest articles on Forem by vishwa@chauhan (@vishwa_9).</description>
    <link>https://forem.com/vishwa_9</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%2F1250869%2F5957594d-e15e-40ec-b81f-908b2f6e90c7.jpg</url>
      <title>Forem: vishwa@chauhan</title>
      <link>https://forem.com/vishwa_9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vishwa_9"/>
    <language>en</language>
    <item>
      <title>this in javascript ?</title>
      <dc:creator>vishwa@chauhan</dc:creator>
      <pubDate>Thu, 11 Jan 2024 19:08:22 +0000</pubDate>
      <link>https://forem.com/vishwa_9/this-in-javascript--3ij1</link>
      <guid>https://forem.com/vishwa_9/this-in-javascript--3ij1</guid>
      <description>&lt;p&gt;In javascript this keyword can have different values in non-strict and strict mode. The value of this depends in which context it appears like, function, global, class&lt;/p&gt;

&lt;p&gt;First we'll explore this in function context&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function whatsThis() {
  return this;
}
console.log(whatsThis() === globalThis);// true

const detailObj = {
  name: 'vishwa'
};

detailObj.whatsThis = whatsThis;
console.log(detailObj.whatsThis() === detailObj); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the value of this depends on how function is called, In first console &lt;code&gt;whatsThis&lt;/code&gt; is accessed on &lt;code&gt;globalThis&lt;/code&gt; so the value of this is &lt;code&gt;globalThis&lt;/code&gt;, In second console &lt;code&gt;whatsThis&lt;/code&gt; is accessed on &lt;code&gt;detailObj&lt;/code&gt; so the value of this is &lt;code&gt;detailObj&lt;/code&gt;.&lt;br&gt;
&lt;strong&gt;So the value of this is the object that is used to call the function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In strict mode if function is accessed on &lt;code&gt;globalThis&lt;/code&gt; the value of this is &lt;code&gt;undefined&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 whatsThis() {
  'use strict'
  return this;
}
console.log(whatsThis() === undefined);// true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Arrow functions:&lt;/strong&gt;&lt;br&gt;
In arrow functions &lt;code&gt;this&lt;/code&gt; retains the value of it's enclosing lexical context &lt;code&gt;this&lt;/code&gt;, arrow functions create the closure over the &lt;code&gt;this&lt;/code&gt; value of it's surrounding scope. so no matter how function is invoked the value of &lt;code&gt;this&lt;/code&gt; is same what it was when the function was created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arrowFunc = () =&amp;gt; this;

console.log(arrowFunc() === globalThis);

const obj = {
  name: 'vishwa'
};

console.log(arrowFunc.call(obj) === globalThis); //true 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Constructors:&lt;/strong&gt;&lt;br&gt;
when a functions is used as a constructor it's &lt;code&gt;this&lt;/code&gt; is bound to the new object created. The value of &lt;code&gt;this&lt;/code&gt; 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 getInfo() {
  this.name = 'vishwa';
  this.job = 'coding';
}

const detail = new getInfo();

console.log(detail); //{"name": "vishwa",  "job": "coding"}

function getInfo2() {
  this.name = 'vishwa';
  return {id: 100};
}

const detail2 = new getInfo2();

console.log(detail2); //{"id": 100}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Class:&lt;/strong&gt;&lt;br&gt;
Class constructors are always called with new, so their behaviour is same as function constructors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Detail{
    constructor() {
        this.name = 'vishwa';
        this.job = 'coding';
    }
}

const detail = new Detail();

console.log(detail); // {name: 'vishwa', job: 'coding'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;bind method&lt;/strong&gt;&lt;br&gt;
Calling bind on function f, creates a new function with the same body as function f, but the value of this is bound to the first argument of bind.&lt;br&gt;
&lt;/p&gt;

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

const obj = {
  a: 'pop'
};

const binded = f.bind(obj);

console.log(binded()); // 'pop'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;this in DOM event handlers:&lt;/strong&gt;&lt;br&gt;
When a function is used as a &lt;code&gt;eventlistener&lt;/code&gt; it's this value is bound to the element on which &lt;code&gt;eventlistenter&lt;/code&gt; is placed&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/vishwa357fe5fc25/embed/GReZGvQ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>&lt;script&gt; | HTML Element</title>
      <dc:creator>vishwa@chauhan</dc:creator>
      <pubDate>Sun, 07 Jan 2024 19:30:40 +0000</pubDate>
      <link>https://forem.com/vishwa_9/-html-element-579a</link>
      <guid>https://forem.com/vishwa_9/-html-element-579a</guid>
      <description>&lt;p&gt;What is script 🤔..💭&lt;/p&gt;

&lt;p&gt;script is an HTML element, which is used to embed executable code, or refer as javascript code.&lt;/p&gt;

&lt;p&gt;This element support multiple attributes, but here we will explore only two &lt;strong&gt;async&lt;/strong&gt; and &lt;strong&gt;defer&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;defer&lt;/strong&gt; : This boolean attribute is set to indicate to a browser, that the script will start downloading in parallel with document parsing, but it will only start executing after the the document content is parsed but before &lt;u&gt;DOMContentLoaded&lt;/u&gt; is fired.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Deferred scripts keep their relative order, just like regular scripts.&lt;/p&gt;

&lt;p&gt;Also it will prevent &lt;u&gt;DOMContentLoaded&lt;/u&gt; from firing until defer script is downloaded and finished executing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;async&lt;/strong&gt; : This attribute is set to indicate the browser, that it will start fetching in parallel to parsing, and will start executing as soon as it's available. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;DOMContentLoaded&lt;/u&gt; and async are independent. If async script is long and taking time to download and document is parsed then &lt;u&gt;DOMContentLoaded&lt;/u&gt; will start it's execution.&lt;/p&gt;

&lt;p&gt;Example: &lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/vishwa357fe5fc25/embed/GReZGvQ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

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