<?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: vinoo</title>
    <description>The latest articles on DEV Community by vinoo (@vinoo).</description>
    <link>https://dev.to/vinoo</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%2F430199%2F94b6d754-df7e-43c2-a5d6-ee3c80517bd5.png</url>
      <title>DEV Community: vinoo</title>
      <link>https://dev.to/vinoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vinoo"/>
    <language>en</language>
    <item>
      <title>How To Setup SSH Keys For Multiple Hosts</title>
      <dc:creator>vinoo</dc:creator>
      <pubDate>Fri, 16 Sep 2022 03:23:58 +0000</pubDate>
      <link>https://dev.to/vinoo/how-to-setup-ssh-keys-for-multiple-hosts-4n1e</link>
      <guid>https://dev.to/vinoo/how-to-setup-ssh-keys-for-multiple-hosts-4n1e</guid>
      <description>&lt;p&gt;SSH is a very powerful way to access remote servers. Learn how to easily setup SSH in your system.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article was originally posted &lt;a href="https://vinoo.hashnode.dev/how-to-setup-ssh-keys-for-multiple-hosts" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are probably hundreds, even thousands of articles on this topic. And that's precisely the reason why it can also get daunting, confusing, and borderline nauseating to understand how to setup, much less use SSH as a beginner. &lt;/p&gt;

&lt;p&gt;This post is aimed to be a no-nonsense, no-fluff guide to creating multiple SSH keys for difference Git based accounts like GitHub or BitBucket, and manage all of them easily, simultaneously. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Disclaimer: This is not an expert guide, and the steps here are simplified to ensure all levels of developers can follow. In case of any discrepancy or incorrect steps, please let me know in the comments and I would be happy to correct. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is recommended to complete the below steps from beginning to end for one key and then repeat the process for all keys thereafter. &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1 - Check for existing keys
&lt;/h3&gt;

&lt;p&gt;Use the command below to check if there are existing SSH keys - &lt;/p&gt;

&lt;p&gt;&lt;code&gt;ls -al ~/.ssh&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;If you need a clean slate and start setting up everything afresh, delete the existing entries with this command - &lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh-add -D&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that this will only delete the manual entries previously done by you and not the ones automatically added by other agents/tools. To ensure that the &lt;code&gt;.ssh&lt;/code&gt; folder is empty, simply delete the contents of the folder like you normally would.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Files beginning with a dot '.' are irrelevant in this context. SSH key names look like this - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi3l1tw2cqdpr8po1r32g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi3l1tw2cqdpr8po1r32g.png" alt="SSH Key Names" width="800" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2 - Generate new SSH key
&lt;/h3&gt;

&lt;p&gt;Type in this command to generate a new SSH key -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh-keygen -t ed25519 -C "your@email.com"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Meaning of different flags in the above command - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-t : defines the type of algorithm that generates the key; RSA, ed25519, etc. ed25519 is a more encrypted, more secure algorithm&lt;/li&gt;
&lt;li&gt;-C : used to define any comment to identify the particular entity who generates the key. Optional, but always recommended &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once entered, the above command generates the SSH key. Make sure you enter a unique name within the path, else the file name will default to &lt;code&gt;id_rsa&lt;/code&gt;. Your screen should resemble like so -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb78cjjyiehpnut1a0o65.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb78cjjyiehpnut1a0o65.png" alt="Generate SSH Key" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hit Enter through the next steps (paraphrase, et al), and your keys will be generated; Two files - a private key and public key will be generated. &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3 - Create a &lt;code&gt;known_hosts&lt;/code&gt; file
&lt;/h3&gt;

&lt;p&gt;Nothing fancy; simply go inside the &lt;code&gt;.ssh&lt;/code&gt; folder with &lt;code&gt;cd .ssh&lt;/code&gt; and create a file named &lt;code&gt;known_hosts_yourkeyname&lt;/code&gt;. This helps you easily diagnose issues should they occur in future. Use the command below to create it - &lt;/p&gt;

&lt;p&gt;&lt;code&gt;touch known_hosts_yourkeyname&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Leave it untouched after this.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4 - Create/Edit the config file
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;config&lt;/code&gt; file is used to define and set options for each key and its corresponding host. Open the &lt;code&gt;.ssh&lt;/code&gt; folder in your preferred editor and create a new file named &lt;code&gt;config&lt;/code&gt; (without any extensions) and paste the following lines into it - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzcdtbwgaslww20f5c185.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzcdtbwgaslww20f5c185.png" alt="Create config file" width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a brief breakdown of what each line means - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Host : This is used to identify each key amidst the different ones in your system. Keep it simple with the URL of the host as above.&lt;/li&gt;
&lt;li&gt;HostName: This is where your remote repository exists. This is what is used to match with your repo's base URL. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;For a default SSH key which is usually a personal one, keep the HostName  entry the same as the Host. For work SSH keys, you can modify the HostName like so - &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi0msu5eml2uzh48etwdx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi0msu5eml2uzh48etwdx.png" alt="Modify HostName" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User : For git based hosts/servers, the entry will be &lt;code&gt;git&lt;/code&gt;. If it is a Digital Ocean Droplet or AWS instance, it may vary accordingly. &lt;/li&gt;
&lt;li&gt;AddKeysToAgent : In a further step, we will add the SSH keys to the &lt;code&gt;ssh-agent&lt;/code&gt; in the system - which is a program that keeps track of identities and passwords, so that you do not have to enter your credentials each time you perform git read/write operations. In this line, we are prompting the private key to be authenticated by the &lt;code&gt;ssh-agent&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;IdentityFile : This is the path of the SSH key&lt;/li&gt;
&lt;li&gt;UserKnownHostsFile : This is used to store all connections to hosts that your key connects to. Specify the name of the &lt;code&gt;known_hosts&lt;/code&gt; file you created earlier using the &lt;code&gt;touch&lt;/code&gt; command. &lt;/li&gt;
&lt;li&gt;IdentitiesOnly : This entry is used to ensure that only the specified keys are used to connect to a host, and no other. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: Once you generate more keys, you will need to simply add the same set of lines as above for those new keys. We will see an example of this at the end of the article.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 5 - Add the keys to &lt;code&gt;ssh-agent&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Enter the command below - &lt;/p&gt;

&lt;p&gt;&lt;code&gt;eval "$(ssh-agent -s)"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A unique Agent PID will be displayed which means that the ssh-agent is now running.&lt;/p&gt;

&lt;p&gt;Next, add the keys to it - &lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh-add --apple-use-keychain ~/.ssh/your_keyname&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you are not on a Mac, do not use the &lt;code&gt;--aple-use-keychain&lt;/code&gt; flag. This is for the newer keychain access management in MacOS versions after Monterey. If you are using an older version or are on Linux, replace it with this - &lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh-add -K ~/.ssh/your_keyname&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You will get a line confirming the key was added. &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6 - Add the key to your host
&lt;/h3&gt;

&lt;p&gt;Make sure you're inside the &lt;code&gt;.ssh&lt;/code&gt; folder. Then, copy the contents of your public key like so - &lt;/p&gt;

&lt;p&gt;&lt;code&gt;cat your_keyname.pub | pbcopy&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;cat&lt;/code&gt; is to display the contents of the public key, and simultaneously, with the pipeline operator, we use &lt;code&gt;pbcopy&lt;/code&gt; to copy the contents to the system's clipboard.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, add this to your host. If you are on GitHub, go to your account Settings&amp;gt;SSH and GPG Keys and add the key. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fre9pc6jkwvqlsfcjf6eg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fre9pc6jkwvqlsfcjf6eg.jpeg" alt="Add SSH Keys" width="800" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7 - Verify and start SSHing
&lt;/h3&gt;

&lt;p&gt;The last step is to verify that your configuration is up and running. Enter this command - &lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh -T git@github.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Suppose you have repeated the process up until Step 6 for two other keys and you now have two keys, with the second key supposedly on GitHub again, and the third key on BitBucket, then your config file should look similar to this - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F847bvzn0gfyhfv3s3tum.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F847bvzn0gfyhfv3s3tum.png" alt="Multiple Hosts on config" width="800" height="791"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, there are multiple hosts with multiple corresponding keys and all of them are pattern matched by the &lt;code&gt;ssh-agent&lt;/code&gt; to connect only as specified. Notice how the &lt;code&gt;Host&lt;/code&gt; entry for GitHub work account and BitBucket account are unique. &lt;/p&gt;

&lt;p&gt;You can now verify the configuration for all of them - &lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh -T git@github.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh -T git@github-work&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh -T git@bitbucket.org&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You should get a success message for each of them indicating that authentication was successful -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Hi some_user! You've successfully authenticated, but GitHub does not provide shell access.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Voila! 🎉 You are now all set to clone your repos from the hosts you have setup. &lt;/p&gt;

&lt;p&gt;An important thing to notice here is that in our example above, we have 2 GitHub hosts where the &lt;code&gt;HostName&lt;/code&gt; entry points to &lt;code&gt;github.com&lt;/code&gt; but the &lt;code&gt;Host&lt;/code&gt; entry is kept different in order for the ssh-agent to uniquely identify each of them. &lt;/p&gt;

&lt;p&gt;Therefore, when you are cloning a repo from the Default Personal account, you can simply type in -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git clone git@github.com:your_username/your_repo_name.git&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;However, when you want to clone a repo from the Work GitHub account, you would have to change the base URL of the repo after the part &lt;code&gt;git@&lt;/code&gt;, with your corresponding &lt;code&gt;Host&lt;/code&gt; entry, like so -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git clone git@github-work:your_username/your_repo_name.git&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If it is a different host altogether like the Bitbucket one, then you can simply use the clone command like usual. &lt;/p&gt;

&lt;p&gt;This brings us to the end of this rather long, but detailed article on how to setup multiple SSH keys for multiple hosts in your system. &lt;/p&gt;

&lt;p&gt;Have fun setting up, and do let me know in the comments, if you have any suggestions. Until next time 🖖&lt;/p&gt;

</description>
      <category>ssh</category>
      <category>github</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Deeper Into this In JavaScript</title>
      <dc:creator>vinoo</dc:creator>
      <pubDate>Thu, 18 Nov 2021 07:49:04 +0000</pubDate>
      <link>https://dev.to/vinoo/deeper-into-this-in-javascript-4lil</link>
      <guid>https://dev.to/vinoo/deeper-into-this-in-javascript-4lil</guid>
      <description>&lt;p&gt;In a &lt;a href="https://dev.to/vinoo/oops-in-javascript-intro-to-creating-objects-and-this-keyword-2bi"&gt;previous article&lt;/a&gt;, we saw how to use &lt;code&gt;this&lt;/code&gt; keyword with objects. In this post, we shall dive deeper into different bindings of &lt;code&gt;this&lt;/code&gt; that we will encounter when dealing with it in functions. Bindings mean the different ways &lt;code&gt;this&lt;/code&gt; behaves in different contexts in a function. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Default Binding
&lt;/h3&gt;

&lt;p&gt;Consider the following example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;defaultThis&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;defaultThis&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since there is no &lt;code&gt;username&lt;/code&gt; variable declared or defined, &lt;code&gt;this&lt;/code&gt; keyword gets the default binding - it references the global &lt;code&gt;Window&lt;/code&gt; object here, as can be seen below -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnehaqafkodobpr5cp6co.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnehaqafkodobpr5cp6co.png" alt="this-one" width="800" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Implicit Binding
&lt;/h3&gt;

&lt;p&gt;This binding is created by the behaviour of the function. Let's take an example to understand -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;hobbit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bilbo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;welcome&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello `&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 

&lt;span class="nx"&gt;hobbit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;welcome&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output would be as exptected - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frnfb640lzamsi5xn4169.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frnfb640lzamsi5xn4169.png" alt="this-two" width="800" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, since there is an object that calls the function &lt;code&gt;welcome()&lt;/code&gt;, &lt;code&gt;this&lt;/code&gt; implicitly refers to the object inside the function. &lt;/p&gt;

&lt;h3&gt;
  
  
  3. Explicit Binding
&lt;/h3&gt;

&lt;p&gt;Explicit binding means to explicitly bind the value of &lt;code&gt;this&lt;/code&gt; to any specific object. &lt;/p&gt;

&lt;p&gt;There are 3 methods to implement explicit binding - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;call()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider the code snippet we used above in Implicit Binding - the property &lt;code&gt;name&lt;/code&gt; and method &lt;code&gt;welcome&lt;/code&gt; are both defined inside the object &lt;code&gt;hobbit&lt;/code&gt;. This makes the binding for &lt;code&gt;this&lt;/code&gt; fairly..implicit 🌝. What if the object is separate from a method? Consider the snippet below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;welcome&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;hobbit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Frodo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;welcome&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Welcome&lt;/span&gt;
&lt;span class="nx"&gt;welcome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hobbit&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Welcome Frodo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first function call &lt;code&gt;welcome()&lt;/code&gt; has no reference to an object, so it would not return anything in the alert statement after &lt;code&gt;Welcome&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The second function call is where we have accessed the object with the &lt;code&gt;call&lt;/code&gt; method. This means that we are specifying to the browser to assign the object &lt;code&gt;hobbit&lt;/code&gt; being passed as parameter to &lt;code&gt;this&lt;/code&gt; using &lt;code&gt;call&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Another use case for &lt;code&gt;call&lt;/code&gt; is that we can pass parameters to signify the value for &lt;code&gt;this&lt;/code&gt; along with arguments for the function. Example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;spellOne&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;spellTwo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; cast the spells &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;spellOne&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;spellTwo&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wizard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ron Weasley&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wizard&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Expelliarmus&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Slugulus Eructo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the function &lt;code&gt;foo&lt;/code&gt; is called with the &lt;code&gt;call&lt;/code&gt; method and the object &lt;code&gt;wizard&lt;/code&gt; is passed as the first argument which automatically gets assigned to &lt;code&gt;this&lt;/code&gt; in the function, along with the rest of the arguments. Note that the first argument always gets assigned to &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The output is as below -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F112ah77n8a0iu3jvmm6m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F112ah77n8a0iu3jvmm6m.png" alt="this-3" width="800" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But there is a drawback for this use case. What if there are tens of arguments to be passed for multiple objects? Very cumbersome 😕 We have the next binding method to improve usability a little better. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;apply()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Take a look at this snippet -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;spellOne&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;spellTwo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; cast the spells &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;spellOne&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;spellTwo&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wizard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ron Weasley&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wizard&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Expelliarmus&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Slugulus Eructo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The format is the same, except that instead of &lt;code&gt;call&lt;/code&gt;, we use the method &lt;code&gt;apply&lt;/code&gt;, and instead of passing the arguments one after the other, we just wrap them in an array. The output remains the same. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;bind()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;bind()&lt;/code&gt; method creates a new function which when invoked, assigns the provided values to &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Take a look at the snippet below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;spellOne&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;spellTwo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; cast the spells &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;spellOne&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;spellTwo&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wizard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ron Weasley&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;castSpell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wizard&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Expelliarmus&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Slugulus Eructo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;castSpell&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we are using &lt;code&gt;bind()&lt;/code&gt; to be referenced by the variable &lt;code&gt;castSpell&lt;/code&gt;, which can then be invoked as a normal function call. &lt;/p&gt;

&lt;p&gt;The advantages of using &lt;code&gt;bind()&lt;/code&gt; are that -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We are explicitly binding the &lt;code&gt;foo()&lt;/code&gt; method to the instance &lt;code&gt;castSpell&lt;/code&gt; such that &lt;code&gt;this&lt;/code&gt; of &lt;code&gt;foo()&lt;/code&gt; is now bound to &lt;code&gt;castSpell&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Even though the &lt;code&gt;wizard&lt;/code&gt; object does not have &lt;code&gt;castSpell&lt;/code&gt; as its property, because we are using &lt;code&gt;bind()&lt;/code&gt;, &lt;code&gt;wizard&lt;/code&gt; now recognises &lt;code&gt;castSpell&lt;/code&gt; as its method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;bind()&lt;/code&gt; returns a new function reference that we can call anytime we want in future. &lt;/p&gt;

&lt;h3&gt;
  
  
  4. new Binding
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;new&lt;/code&gt; binding is used specifically for constructor functions. Take a look below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Wizard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;spell&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;spell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;spell&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;intro&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hermione&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`The witch &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; cast the spell &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;spell&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`The wizard &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; cast the spell &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;spell&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;hermione&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Wizard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hermione&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Occulus Reparo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;ronald&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Wizard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ronald&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Slugulus Erecto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Constructor functions are special functions that are used to create new objects. The use of &lt;code&gt;new&lt;/code&gt; keyword means that we are creating a new object (or instance) of the (constructor) function.&lt;/p&gt;

&lt;p&gt;Whenever &lt;code&gt;new&lt;/code&gt; is used before any constructor function (name with the Capitalized convention followed), the JS engine undertands that &lt;code&gt;this&lt;/code&gt; inside the function will always point to the empty object created by &lt;code&gt;new&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  5. HTML Element Event Binding
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; can be used to bind the values of specific events or elements in HTML.&lt;/p&gt;

&lt;p&gt;Take a look at this example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; 
&lt;span class="na"&gt;class =&lt;/span&gt;&lt;span class="s"&gt;"this-one"&lt;/span&gt;
&lt;span class="na"&gt;onclick=&lt;/span&gt;&lt;span class="s"&gt;"console.log(this)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
this One
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;this&lt;/code&gt; will always bind itself to the element where the event happened; in this case, the &lt;code&gt;this-one&lt;/code&gt; class button.&lt;/p&gt;

&lt;p&gt;The output will be as below -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv6xbt6bxaoalqjmp8f2h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv6xbt6bxaoalqjmp8f2h.png" alt="this-4" width="800" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now take a look at this snippet -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; 
&lt;span class="na"&gt;class =&lt;/span&gt;&lt;span class="s"&gt;"this-two"&lt;/span&gt;
&lt;span class="na"&gt;onclick=&lt;/span&gt;&lt;span class="s"&gt;"this.style.backgroundColor='orange'"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
this Two
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;this&lt;/code&gt; is again bound to the button with the class &lt;code&gt;this-two&lt;/code&gt;, and the &lt;code&gt;onclick&lt;/code&gt; event happens only on that specific button.&lt;/p&gt;

&lt;p&gt;Output - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj2qbfjtvr12g7i1k93j0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj2qbfjtvr12g7i1k93j0.png" alt="this-5" width="304" height="98"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How about when we call a function within the element?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; 
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="err"&gt;="&lt;/span&gt;&lt;span class="nc"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;three&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;
onclick=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="nf"&gt;changeColor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;gt;
this Three
&amp;lt;/button&amp;gt;

&amp;lt;script&amp;gt;
  function changeColor() {
    console.log(this);
  }
&amp;lt;/script&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we are calling the &lt;code&gt;console.log()&lt;/code&gt; function along with &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, the value of &lt;code&gt;this&lt;/code&gt; is as below -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faad3fynepg0c5broo26w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faad3fynepg0c5broo26w.png" alt="this-6" width="800" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, &lt;code&gt;this&lt;/code&gt; points to the global &lt;code&gt;Window&lt;/code&gt; object. We can see that Default Binding occurs here since the function &lt;code&gt;changeColor()&lt;/code&gt; is called without a prefix.&lt;/p&gt;




&lt;p&gt;&lt;code&gt;this&lt;/code&gt; is definitely strange. However, the use cases provide us with flexibility to use objects effectively. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Function.Prototype in JavaScript</title>
      <dc:creator>vinoo</dc:creator>
      <pubDate>Sun, 14 Nov 2021 12:15:27 +0000</pubDate>
      <link>https://dev.to/vinoo/functionprototype-in-javascript-305j</link>
      <guid>https://dev.to/vinoo/functionprototype-in-javascript-305j</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article was originally published &lt;a href="https://ivinoop.hashnode.dev/functionprototype-in-javascript" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the &lt;a href="https://ivinoop.hashnode.dev/oops-in-javascript-what-is-dunder-proto" rel="noopener noreferrer"&gt;previous article&lt;/a&gt; , we saw how objects behave and what Dunder Proto means. In this post, let us discuss why Dunder Proto is used and how it can help us write better, optimised code. &lt;/p&gt;

&lt;p&gt;Dunder Proto is mainly used for two cases - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To manage user methods for the objects that are created at runtime&lt;/li&gt;
&lt;li&gt;To increase usability through better memory management&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  So how exactly does this efficiency happen?
&lt;/h3&gt;

&lt;p&gt;We know that &lt;code&gt;__proto__&lt;/code&gt; is a special property present in every object that we create in JavaScript. This property presents (and holds) different methods/key-value pairs for each object being created. &lt;/p&gt;

&lt;p&gt;And since every function is also an object, each function holds a set of methods as well that can be invoked right off the bat (like &lt;code&gt;map()&lt;/code&gt;, &lt;code&gt;filter()&lt;/code&gt;, etc). &lt;/p&gt;

&lt;p&gt;Here lies the advantage - you can (and should!) put all your methods in one place, in the Dunder Proto. &lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because since it is already an existing property present in every object, there is no need to explicitly create a variable to manage those methods. Think about it - with each object you create, a whole list of methods get attached to it, leading to a bigger mess in terms of memory management. But by putting it all in the special bag that is the Dunder Proto, it is implicitly managed. &lt;/p&gt;

&lt;p&gt;Example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userMethods&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="na"&gt;changeName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userMethods&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As can be seen, the methods &lt;code&gt;sayHello()&lt;/code&gt; and &lt;code&gt;changeName()&lt;/code&gt; are both put in a single object variable, which is then assigned to a variable using &lt;code&gt;Object.create()&lt;/code&gt; method that accepts this object of methods as a parameter. These are now stored in &lt;code&gt;__proto__&lt;/code&gt; as shown below -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2k4ba1akwqduwxrzftyb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2k4ba1akwqduwxrzftyb.png" alt="dunder_proto_example_1" width="800" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Neat, right? 🥳&lt;/p&gt;

&lt;p&gt;There is another property that exists to make your job even more easy, and the code more organised. Say hello to &lt;code&gt;F.prototype&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function.Prototype
&lt;/h2&gt;

&lt;p&gt;In the previous ways of creating and using object and methods, we used separate variables to store methods and object data. As our application becomes more complex, there are chances of code going out of hand. Using function prototypes can help us organise our code better.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is function prototype?
&lt;/h3&gt;

&lt;p&gt;Just like every object has a special property called Dunder Proto, every function in JavaScript also has a property called the Function Protoype. The use case for this property is that since it is a property of a function, it has its own Dunder Proto as well. Take a look below for clarity - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzggopx0h4fqbbhwrwyb4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzggopx0h4fqbbhwrwyb4.png" alt="dunder_proto_example_2" width="656" height="638"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the function &lt;code&gt;foo()&lt;/code&gt; has the &lt;code&gt;prototype()&lt;/code&gt; property which in turn contains the &lt;code&gt;__proto__&lt;/code&gt; property. &lt;/p&gt;

&lt;p&gt;This means that there is an even better bag to hold all our object data and methods in one place without the need for a separate function to create objects and a separate variable/object to hold methods. Using the &lt;code&gt;function_name.protoype&lt;/code&gt; property, we can push all the data and methods to be stored in one single bag, for any number of objects that are created in future. &lt;/p&gt;

&lt;p&gt;Example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Hobbit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;Hobbit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;                  &lt;span class="c1"&gt;// Using the function.prototype property to put object's methods&lt;/span&gt;
  &lt;span class="na"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hobbit's name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below, we can see that the methods as well as data are collected inside this &lt;code&gt;F.prototype&lt;/code&gt; property. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fitatuaczvwo93kmk3twf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fitatuaczvwo93kmk3twf.png" alt="dunder_proto_example_3" width="800" height="688"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The question here is - we used the &lt;code&gt;F.prototype&lt;/code&gt; property to store the methods and data; but how did we instantiate the data to be stored along with the methods?&lt;/p&gt;

&lt;p&gt;This is where the &lt;code&gt;new&lt;/code&gt; keyword in JavaScript comes into picture. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;new&lt;/code&gt; keyword is used to create an "instance" of an object. An instance here means that -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A new object is created&lt;/li&gt;
&lt;li&gt;The methods are added to the &lt;code&gt;prototype&lt;/code&gt; property of the function&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;this&lt;/code&gt; keyword automatically binds the newly created property to the object (and its prototype)&lt;/li&gt;
&lt;li&gt;The newly created object is then returned as well&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Like below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;hobbitOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Hobbit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Samwell&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it - a simple &lt;code&gt;new&lt;/code&gt; keyword to make code (and life) easier 😁&lt;/p&gt;

&lt;p&gt;Notice that the code above looks almost the same as the previous method of object creation, except that the object is now returned implicitly with the usage of &lt;code&gt;new&lt;/code&gt; keyword, as well as the data and methods are managed under a single property with the use of &lt;code&gt;Function.prototype&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;Confused much? 😐 I would expect so; JavaScript is by no means easy. However, the way it offers such flexibility in writing code is what makes it interesting.&lt;/p&gt;

&lt;p&gt;In the next article, I go into &lt;code&gt;class&lt;/code&gt;, &lt;code&gt;constructor&lt;/code&gt; and how our code can be even more organised than ever, binding all this knowledge neatly. Stay tuned to clear all your confusion 🙂&lt;/p&gt;

&lt;p&gt;Until next time 🤠 Keep shipping 🚀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>OOPs in JavaScript - Deeper Into Objects &amp; Dunder Proto</title>
      <dc:creator>vinoo</dc:creator>
      <pubDate>Wed, 27 Oct 2021 14:18:17 +0000</pubDate>
      <link>https://dev.to/vinoo/oops-in-javascript-deeper-into-objects-dunder-proto-53g3</link>
      <guid>https://dev.to/vinoo/oops-in-javascript-deeper-into-objects-dunder-proto-53g3</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;In this post, originally published &lt;a href="https://ivinoop.hashnode.dev/oops-in-javascript-what-is-dunder-proto" rel="noopener noreferrer"&gt;here&lt;/a&gt; we shall dive a bit deeper into nature of objects, and cover the Dunder Proto concept. &lt;/p&gt;

&lt;p&gt;This post is part of the  &lt;a href="https://ivinoop.hashnode.dev/series/javascript-advanced" rel="noopener noreferrer"&gt;Advanced JavaScript series&lt;/a&gt; where I try to explain essential advanced JS concepts in a simple manner. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Nature of Objects
&lt;/h3&gt;

&lt;p&gt;Consider the below object -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;details&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Richard Hendricks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;company&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pied Piper&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above object, if we try to access the property &lt;code&gt;company&lt;/code&gt;, it is possible since &lt;code&gt;company&lt;/code&gt; is an existing property of the &lt;code&gt;details&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;However, the below snippet would return &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 javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;details&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;designation&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because there is no property named &lt;code&gt;designation&lt;/code&gt; inside &lt;code&gt;details&lt;/code&gt;. This is exactly how we would expect an object to behave. &lt;/p&gt;

&lt;p&gt;However, take a look at the example below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output would be as below - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvg33qm7fk9soyhvq3t6j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvg33qm7fk9soyhvq3t6j.png" alt="objects-in-detail" width="736" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But &lt;code&gt;map()&lt;/code&gt; is not a method inside &lt;code&gt;arr&lt;/code&gt;. So how is this being computed and where is this coming from?&lt;/p&gt;

&lt;h3&gt;
  
  
  Dunder Proto &lt;code&gt;__proto__&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Inside every object in JavaScript lies a special property called &lt;code&gt;Dunder Proto&lt;/code&gt;. The name is coined due to the way this object is represented - &lt;code&gt;__proto__&lt;/code&gt; (accompanied by double underscore on both sides of the word &lt;code&gt;proto&lt;/code&gt;). &lt;/p&gt;

&lt;p&gt;As we can see in the above image, the object &lt;code&gt;arr&lt;/code&gt; (and basically every object you create in JS), has the &lt;code&gt;[[Prototype]]:Array&lt;/code&gt; property, inside which lies &lt;code&gt;__proto__&lt;/code&gt;. If we expand this &lt;code&gt;[[Prototype]]: Array&lt;/code&gt; property in our example, we should be able to see &lt;code&gt;__proto__&lt;/code&gt;, which in turn contains a huge list of methods like &lt;code&gt;every&lt;/code&gt;, &lt;code&gt;forEach&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;splice&lt;/code&gt;, etc. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuol5blh1h5tgraqbfew3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuol5blh1h5tgraqbfew3.png" alt="objects-in-detail" width="738" height="1244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The point to be noted here is that each object we create has a different set of key-value pairs in the &lt;code&gt;__proto__&lt;/code&gt; property. &lt;/p&gt;

&lt;p&gt;Whenever we try to call/access a property that does not exist in the defined object, the JS engine goes down the &lt;code&gt;__proto__&lt;/code&gt; chain (or a rabbit 🐇 hole), to search for that property. In the above case, we tried to compute the &lt;code&gt;map()&lt;/code&gt; method on an array (which is an object), and it went down the &lt;code&gt;__proto__&lt;/code&gt; chain to look for the same. &lt;/p&gt;

&lt;p&gt;This is how the hidden nature of object allows for all array, object, and string methods to be carried out. &lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;__proto__&lt;/code&gt; is a special property of an object, it can be accessed as well. Suppose you want to add a new property under &lt;code&gt;__proto__&lt;/code&gt; to the &lt;code&gt;details&lt;/code&gt; object above, this is how to do it -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;details&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;alertMsg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello Dunder Proto =&amp;gt; __proto__`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function is now added to the &lt;code&gt;__proto__&lt;/code&gt; property as can be seen below - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwzmbwfncijkbh1bpxffi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwzmbwfncijkbh1bpxffi.png" alt="objects-in-detail" width="736" height="758"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;We learnt a hidden nature of objects in JavaScript, and the basics of Dunder Proto. In the next post, we shall learn about why and where Dunder Proto can be used to make our code more efficient. &lt;/p&gt;

&lt;p&gt;Until next time! 🙌&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>OOPs in JavaScript - Intro to Creating Objects and `this` Keyword</title>
      <dc:creator>vinoo</dc:creator>
      <pubDate>Mon, 25 Oct 2021 05:13:03 +0000</pubDate>
      <link>https://dev.to/vinoo/oops-in-javascript-intro-to-creating-objects-and-this-keyword-2bi</link>
      <guid>https://dev.to/vinoo/oops-in-javascript-intro-to-creating-objects-and-this-keyword-2bi</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article as originally published &lt;a href="https://ivinoop.hashnode.dev/oops-in-javascript-creating-objects-and-this-keyword" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;JavaScript is a flexible, object oriented language. This means that it allows developers to create different  kinds of objects for different purposes. Almost everything in JavaScript (except Primitive types) is an object (Booleans, Strings, Numbers, Dates, Arrays, Functions, Objects, etc). &lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Objects
&lt;/h2&gt;

&lt;p&gt;Let us look at the different ways to create an object in JS. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Object Literal
&lt;/h3&gt;

&lt;p&gt;This is the most popular and easiest way of creating objects. Object literal consists of the type of variable/object name (let, var, or const), object name, and the collection of properties inside it. Here's an example to understand it better -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt; &lt;span class="c1"&gt;// Object Literal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A more detailed object literal -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userDetails&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Arya&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Stark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;occupation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Girl With No Name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Object Constructor
&lt;/h3&gt;

&lt;p&gt;Constructors are special functions that are called when an object is created with the &lt;code&gt;new&lt;/code&gt; keyword. Take a look at the example below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; 
&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Targaryen&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;occupation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dragon Rider&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the keyword &lt;code&gt;new&lt;/code&gt; is used along with the case-sensitive keyword &lt;code&gt;Object&lt;/code&gt;, indicating that it is a special keyword used in creating objects through Object Constructor method. &lt;/p&gt;

&lt;p&gt;The result is the same as creating object through Object Literals.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Object.create
&lt;/h3&gt;

&lt;p&gt;This is another method to create new objects, which gives us more control over handling them. &lt;code&gt;Object.create&lt;/code&gt; accepts a parameter, which can be either &lt;code&gt;null&lt;/code&gt; or an object (key-value pairs).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Vinoo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;designation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Developer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we pass &lt;code&gt;null&lt;/code&gt; as the parameter, it still creates an empty object. &lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;this&lt;/code&gt; keyword
&lt;/h2&gt;

&lt;p&gt;In simple words, &lt;code&gt;this&lt;/code&gt; is a special predefined variable that is present in every function declaration. The value of &lt;code&gt;this&lt;/code&gt; variable changes according to the way we call the function in which it is defined. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; is used in both global and function contexts. It always points to an object. More specifically, &lt;code&gt;this&lt;/code&gt; references the object that is currently calling the function. &lt;/p&gt;

&lt;p&gt;Example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;addition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;add5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addition&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;addition()&lt;/code&gt; is a function which is a property of the &lt;code&gt;add5&lt;/code&gt; object (a function inside a property becomes a method). Hence, inside the &lt;code&gt;addition()&lt;/code&gt; &lt;strong&gt;method&lt;/strong&gt;, &lt;code&gt;this&lt;/code&gt; references the &lt;code&gt;add5&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Whenever we call a function through an object ( e.g -&lt;code&gt;add5.addition()&lt;/code&gt;), &lt;code&gt;this&lt;/code&gt; will always point to that object. &lt;/p&gt;

&lt;p&gt;In Global context, &lt;code&gt;this&lt;/code&gt; refers to the &lt;strong&gt;global object&lt;/strong&gt;, which is the &lt;code&gt;window&lt;/code&gt; object in a web browser. &lt;/p&gt;

&lt;p&gt;If we run the following snippet, it can be seen that&lt;code&gt;this&lt;/code&gt; points to the &lt;code&gt;window&lt;/code&gt; global object -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Essentially, if a property is assigned to &lt;code&gt;this&lt;/code&gt;, then that property is added to the global object, and can be accessed by the &lt;code&gt;window&lt;/code&gt; object. &lt;/p&gt;

&lt;p&gt;Example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will be &lt;code&gt;21&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;&lt;code&gt;this&lt;/code&gt; keyword has more use cases in the Function context, which we shall see in an upcoming post. &lt;/p&gt;

&lt;p&gt;Stay tuned! 🚀&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript In Snippets - Intro and Fundamentals</title>
      <dc:creator>vinoo</dc:creator>
      <pubDate>Sat, 06 Mar 2021 08:24:51 +0000</pubDate>
      <link>https://dev.to/vinoo/javascript-in-snippets-part-1-intro-and-fundamentals-25m3</link>
      <guid>https://dev.to/vinoo/javascript-in-snippets-part-1-intro-and-fundamentals-25m3</guid>
      <description>&lt;h1&gt;
  
  
  The History
&lt;/h1&gt;

&lt;p&gt;There are cults.&lt;/p&gt;

&lt;p&gt;There are followers.&lt;/p&gt;

&lt;p&gt;And then, there is an entire world embroiled in JavaScript. A world full of engineers, developers, and designers - all ardently worshipping a language that has taken the web world by storm. Again and again.&lt;/p&gt;

&lt;p&gt;JS was introduced to the world 26 years ago. I was introduced to it 9 years ago in a college classroom, and I felt.. nothing. It was taught as just another programming language, with almost the same syntax as the couple of other languages I knew, and to perform the familiar operations of "finding prime numbers", "generating Fibonacci sequence", etc. I learned the "how" but never the "what" or "why" of JS.&lt;/p&gt;

&lt;p&gt;I admit, rather embarrassingly, that back then I did not even know that JS was a language that was developed for the web. Ignorance is NOT bliss 😐.&lt;/p&gt;

&lt;p&gt;It was originally created for a browser called Netscape Navigator which was competing with Internet Explorer in 1995-1996 to take over the browser domain. Eventually, Internet Explorer won the battle and became the dominant browser at large (back then).&lt;/p&gt;

&lt;p&gt;I know right? IE, of all browsers. Yikes!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyfirtf6ii0hibcnhloaw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyfirtf6ii0hibcnhloaw.jpg" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JS slowly evolved to become a higher end programming language that could breathe interactivity to the browser world, and thus began its many avatars to come.&lt;/p&gt;

&lt;p&gt;As popularity for JS started growing, ECMA (European Computer Manufacturers Association) was handed over the responsibility of overseeing JS' development, restructuring, and maintenance. The name was changed from JavaScript to ECMAScript, but the former name has remained attached to the language to this day.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Present
&lt;/h1&gt;

&lt;p&gt;Undeniably, JS has taken over the world of web as the main language, arguably beating its competitors by a huge factor. Gone are the days when it was only used for just interactivity. Today, we have a plethora of libraries and frameworks that have helped create an entire ecosystem around JS.&lt;/p&gt;

&lt;p&gt;Such is the capability and demand that the masses and the indie hackers no less than revere this language for the intuitive, sleek, and nearly flawless experience the web provides today. All hail JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7n9svut8qq5yxuqi5sfm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7n9svut8qq5yxuqi5sfm.jpg" alt="Alt Text" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  The Learning
&lt;/h1&gt;

&lt;p&gt;After years of forgetting the language and its fundamentals, I restarted my journey into the world of web development. This post will cover a few of the fundamentals, and the next series of posts will document my learning journey as I tackle the web world with JS.&lt;/p&gt;

&lt;p&gt;Let's dive in!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This post is not be a comprehensive one, nor explains the underlying concepts. This is, as the title suggests, a collection of snippets to refer to as a documentation in times of "developer peril😶".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Say Hello
&lt;/h2&gt;

&lt;p&gt;The first order of things while learning a new language (or relearning for that matter), is to say Hello to the World. It's a time tested tradition for all newbies. However, considering JS is a whole other world (universe?) in itself, let's greet it instead!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F156u1tuhd181571b4enl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F156u1tuhd181571b4enl.png" alt="Alt Text" width="754" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;alert()&lt;/strong&gt; is a utility function that is essentially used to display a message in the browser. We shall come back to this later.&lt;/p&gt;

&lt;p&gt;Another nifty little operator that is helpful is &lt;strong&gt;typeof()&lt;/strong&gt;. It is used to identify the data type of a particular expression or variable in use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo4xkay5rzj3rwtp0aecr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo4xkay5rzj3rwtp0aecr.png" alt="Alt Text" width="373" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, the variable a holds &lt;strong&gt;a&lt;/strong&gt; value of 23. When we test with the &lt;strong&gt;typeof(&lt;/strong&gt;) operator, the value is identified as a &lt;strong&gt;number&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Value Types
&lt;/h2&gt;

&lt;p&gt;There are 2 types of value types in JS:&lt;/p&gt;

&lt;h3&gt;
  
  
  Primitive Value Types
&lt;/h3&gt;

&lt;p&gt;These are types that can collect / hold only one value. There are 5 Primitive types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Number&lt;/strong&gt; -  A number type is any integer or whole number, including decimals. &lt;em&gt;Examples: 44, -67, 41.67844, 3.1417258, 0.56&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;String&lt;/strong&gt; - String type consists of letters and words encased in any of these quotes:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9utumeqxmh7zmmt02ysl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9utumeqxmh7zmmt02ysl.png" alt="Alt Text" width="800" height="538"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Boolean&lt;/strong&gt; - Boolean data type is a function that can have only one of two values. These are binary in nature. &lt;em&gt;Examples: true or false, 0 or 1, ON or OFF&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Undefined&lt;/strong&gt; - Undefined is a type that indicates that the variable in question is either not assigned a value or is not declared. This is better illustrated below -&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmjsfvr154witw4zqjr43.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmjsfvr154witw4zqjr43.png" alt="Alt Text" width="800" height="538"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, we see that when we declare variable &lt;strong&gt;a&lt;/strong&gt;, the status is &lt;strong&gt;undefined&lt;/strong&gt; since there is no assigned value. When we check for the &lt;strong&gt;type&lt;/strong&gt; of this undefined variable, the value returned is &lt;strong&gt;"undefined"&lt;/strong&gt;, which is the primitive value of &lt;strong&gt;undefined&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Null&lt;/strong&gt; - Much like undefined, null is also a type that indicates the absence of any value to a variable. However, unlike undefined, null does not have a value "type" to return. When tested for its type in the console, it returns the type as "object" which is what it is treated as while calling objects that are often not relevant.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Non-Primitive Value Type
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Object&lt;/strong&gt; - This is collection of different values of different data types in a single variable. The syntax is as follows -&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffpb604xalrnrazwexv4x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffpb604xalrnrazwexv4x.png" alt="Alt Text" width="800" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As can be seen above, the values for the variable &lt;strong&gt;userDetails&lt;/strong&gt; are enclosed in curly brackets. The values themselves are stored in what are known as &lt;strong&gt;key-value pairs&lt;/strong&gt;. Here, &lt;strong&gt;userName&lt;/strong&gt; is a key and "Vinoo" its corresponding value. Together, they are a &lt;strong&gt;key-value pair&lt;/strong&gt;. Same goes with the &lt;strong&gt;userID&lt;/strong&gt; value as well. In the image below, we can see that the console shows the result for the &lt;strong&gt;typeof&lt;/strong&gt; value of &lt;strong&gt;userDetails&lt;/strong&gt; as "object" data type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxgwccrjmfhjo5ux8l25.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxgwccrjmfhjo5ux8l25.png" alt="Alt Text" width="373" height="155"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;This concludes the JS In Snippets post. Thanks for reading, and stay tuned for more articles on JavaScript. Keep Shipping!&lt;/p&gt;

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