<?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: Muhammad Usman</title>
    <description>The latest articles on DEV Community by Muhammad Usman (@usmaniqbal998).</description>
    <link>https://dev.to/usmaniqbal998</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%2F438143%2F59d35657-7ff4-434f-b54c-61894941f61d.jpeg</url>
      <title>DEV Community: Muhammad Usman</title>
      <link>https://dev.to/usmaniqbal998</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/usmaniqbal998"/>
    <language>en</language>
    <item>
      <title>The 'new' keyword in JS</title>
      <dc:creator>Muhammad Usman</dc:creator>
      <pubDate>Sun, 11 Apr 2021 10:38:17 +0000</pubDate>
      <link>https://dev.to/usmaniqbal998/the-new-keyword-in-js-2ac6</link>
      <guid>https://dev.to/usmaniqbal998/the-new-keyword-in-js-2ac6</guid>
      <description>&lt;p&gt;This is first post of my brand new series &lt;strong&gt;Javascript In Words&lt;/strong&gt;. In this series i would be addressing small and basic parts of javascript that are sometimes overlooked by newbies.   &lt;/p&gt;

&lt;p&gt;In this post I would be discussing the &lt;strong&gt;&lt;em&gt;new keyword&lt;/em&gt;&lt;/strong&gt; in javascript . &lt;strong&gt;&lt;em&gt;new keyword&lt;/em&gt;&lt;/strong&gt; have multiple aspects related to it but for now lets just consider the most basic functionality the &lt;strong&gt;&lt;em&gt;new keyword&lt;/em&gt;&lt;/strong&gt; performs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It creates a new Object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yes that’s it . Not so difficult to grasp right ? &lt;strong&gt;&lt;em&gt;new keyword&lt;/em&gt;&lt;/strong&gt; creates a new empty object . Enough of talking lets dive in to code, and create a &lt;em&gt;'Person'&lt;/em&gt; Object&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const Person = new Object();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What this single line of code did is created an empty object called &lt;em&gt;'Person'&lt;/em&gt;. By empty I mean there are no properties attached to it , its literally empty like &lt;code&gt;Person={}&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;We can add new properties to this object&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Person.firstName=”John”;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and we can access these properties like we normally do&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(Person.firstName) //prints John on console&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;lets see the typeof Person created with &lt;strong&gt;&lt;em&gt;new keyword&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(typeof(Person));&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Yes, you guessed it right!! it prints &lt;strong&gt;object&lt;/strong&gt; as its type on console. So the most basic thing that &lt;strong&gt;&lt;em&gt;new keyword&lt;/em&gt;&lt;/strong&gt; performs is &lt;em&gt;create an empty object&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You must be wondering that what in the line &lt;code&gt;const person = new Object();&lt;/code&gt; what the Object() keyword is? and why it's used in combination to the &lt;code&gt;new&lt;/code&gt; keyword ? , well don’t worry lets explore this &lt;/p&gt;

&lt;h2&gt;
  
  
  Object Method, Object()
&lt;/h2&gt;

&lt;p&gt;Object() also known as Object Constructor or Object Method is default constructor method provided by javascript to create objects. Javascript provide us ability to create our own object constructors and create new objects from its type.  Don't believe me ? let's give it a try.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Person(name,age,profession){
this.firstName=name;
this.age=age;
this.profession=profession
}

const john = new Person(“john”,23,”Teacher”);
console.log(john.firstName) //prints John
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we did is  instead of using a default constructor provided by JS to us we created our own constructor method "Person" and then created an object of Person type out of it.&lt;/p&gt;

&lt;p&gt;We can also create our Object like this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const john = new Person();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Creating an object with empty constructor would initialise its default properties with &lt;em&gt;undefined&lt;/em&gt; and not &lt;em&gt;null&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;I know the &lt;em&gt;this&lt;/em&gt; keyword is bothering you but don't worry i will explain it next and keep it short. &lt;/p&gt;

&lt;h2&gt;
  
  
  'new' and the 'this' Keyword
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;this keyword&lt;/em&gt;&lt;/strong&gt; is treated differently depending on the execution context but for now lets only discuss the relation between &lt;em&gt;this&lt;/em&gt; and the &lt;em&gt;new&lt;/em&gt; keywords&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"new keyword binds this to object itself"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If we consider our "Person" example , new keyword makes &lt;strong&gt;this&lt;/strong&gt; point to the object itself . So it means that the value of &lt;strong&gt;this&lt;/strong&gt; is the object that is being created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function person(name,age,profession){
this.name=name;
this.age=age;
this.profession=profession
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So for only understanding in back of our mind, behind the scenes we can replace &lt;em&gt;this&lt;/em&gt; with "Person" object where ever we encounter &lt;em&gt;this&lt;/em&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 Person(name,age,profession){
john.name=name;
john.age=age;
john.profession=profession;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which makes sense how properties are assigned to our newly created object dynamically, right ? So &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The value of this, when used in an object, is the object itself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can justify this statement with following example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function foo(name){

this.name=name;

printName(){

console.log(this.name)

      }

}

 const myobj = new foo("John");

 myobj.printName(); // Prints John

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

&lt;/div&gt;



&lt;p&gt;We didn't supplied any value to our &lt;em&gt;printName&lt;/em&gt; function but still it prints "John" on the screen due to the fact that &lt;strong&gt;this&lt;/strong&gt; points to our &lt;em&gt;myobj&lt;/em&gt; object and value of &lt;code&gt;this.name&lt;/code&gt; is therefore the value of &lt;code&gt;myobj.name&lt;/code&gt; i.e "John"&lt;/p&gt;

&lt;p&gt;Lets break everything we learned so far in to steps and understand how things work when you create an object with constructor method and &lt;strong&gt;&lt;em&gt;new keyword&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const john = new Person("John",23,""Teacher);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;step 1: &lt;/p&gt;

&lt;p&gt;An empty object is created from the &lt;em&gt;Person&lt;/em&gt; constructor named "John".&lt;/p&gt;

&lt;p&gt;step2:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;this&lt;/strong&gt; points to the newly created object &lt;code&gt;john&lt;/code&gt; and initialise properties of the &lt;code&gt;john&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Thats all for this topic. If you like this don't forget to share this and follow me . For any questions you can comment on the post or reach me out and i will be happy to assist you 😀.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>100daysofcode</category>
      <category>javascriptinwords</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
