<?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: Albert Baliński</title>
    <description>The latest articles on DEV Community by Albert Baliński (@balinskia).</description>
    <link>https://dev.to/balinskia</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%2F368859%2Fe3340ce7-97d4-443c-963f-3c39bc755e2d.jpeg</url>
      <title>DEV Community: Albert Baliński</title>
      <link>https://dev.to/balinskia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/balinskia"/>
    <language>en</language>
    <item>
      <title>The new keyword in Javascript</title>
      <dc:creator>Albert Baliński</dc:creator>
      <pubDate>Sat, 18 Apr 2020 07:53:49 +0000</pubDate>
      <link>https://dev.to/balinskia/the-new-keyword-in-javascript-1och</link>
      <guid>https://dev.to/balinskia/the-new-keyword-in-javascript-1och</guid>
      <description>&lt;p&gt;In this article, I will explain the basic concept, which is the &lt;code&gt;new&lt;/code&gt; keyword.&lt;br&gt;
In Java language, we have a class, which is a blueprint from which you can create an instance. An object is an instance of a class. But in Javascript, technically speaking, there is no such thing as a class. Even though we have the class keyword in the newer versions of Javascript, it is just a special syntactic sugar added in ES6 to make it easier to declare and inherit complex objects. Classes in JavaScript are a special syntax for its prototypical inheritance model that is a comparable inheritance in class-based object-oriented languages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function f() {
  return 5
}
typeof f() // number
typeof new f() // object
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, adding the new keyword creates a new object. Why do we need it? Let’s run the code below (in a browser).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Person(name) { 
  this.name = name
}
const person1 = Person('James')
console.log(person1.name) // undefined 
console.log(window.name) // James 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Why?
&lt;/h3&gt;

&lt;p&gt;Remember that Javascript (just like Python) is a scripting language – the interpreter reads the code and executes it line by line. In the code above, this refers to changes every time the execution context is changed – in the Person function it points to the global object. What we need here is a new object, which we can create using &lt;code&gt;{}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var person1 = {}
Person.call(person1, 'james')
console.log(person1.name) // james
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Above we used the call method, which calls a function with a given this value and arguments provided individually. Or, much simpler, we could do it in one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person2 = new Person('Mary')
console.log(person2.name) // Mary
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By adding the &lt;code&gt;new&lt;/code&gt; keyword, we return a new object to which &lt;code&gt;this&lt;/code&gt; is pointing to, making the code working as expected.!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
