<?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: Shane Taylor (he/him)</title>
    <description>The latest articles on DEV Community by Shane Taylor (he/him) (@shaneallantaylor).</description>
    <link>https://dev.to/shaneallantaylor</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%2F138789%2F4bf3a28f-746e-4d03-9d0a-0299be6cb020.jpeg</url>
      <title>DEV Community: Shane Taylor (he/him)</title>
      <link>https://dev.to/shaneallantaylor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shaneallantaylor"/>
    <language>en</language>
    <item>
      <title>The new keyword</title>
      <dc:creator>Shane Taylor (he/him)</dc:creator>
      <pubDate>Sun, 19 May 2019 23:31:55 +0000</pubDate>
      <link>https://dev.to/shaneallantaylor/micro-concepts-in-object-oriented-programming-the-new-keyword-3ikh</link>
      <guid>https://dev.to/shaneallantaylor/micro-concepts-in-object-oriented-programming-the-new-keyword-3ikh</guid>
      <description>&lt;h2&gt;
  
  
  Let's make use of some syntactical sugar - &lt;code&gt;new&lt;/code&gt;.
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; keyword does a bunch of neat shit for us, but to fully appreciate what it does, we've gotta review something else real quick:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Functions in JavaScript are both objects &lt;strong&gt;and&lt;/strong&gt; functions.&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;This means that this code &lt;strong&gt;actually works&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sayHey&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Heeeeey&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="nx"&gt;sayHey&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tang&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sayHey&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// will log 'Tang' to the console&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Cool!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So functions are both objects and functions - we can access the function bit with parentheses (&lt;code&gt;sayHey()&lt;/code&gt;) and we can access the object bit with dot notation (&lt;code&gt;sayHey.something&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The object portion of functions is &lt;strong&gt;always&lt;/strong&gt; there and it &lt;strong&gt;always&lt;/strong&gt; has a prototype key in it. And the value stored at the prototype key? &lt;em&gt;It's a big ol' empty object and it's exactly where we want to store our functions.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now we need to talk about the cool shit &lt;code&gt;new&lt;/code&gt; does for us in JavaScript. The &lt;code&gt;new&lt;/code&gt; keyword modifies the execution context of the function that immediately follows it by doing the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It creates a property labeled &lt;code&gt;this&lt;/code&gt; and assigns an empty object to that label&lt;/li&gt;
&lt;li&gt;On the &lt;code&gt;this&lt;/code&gt; object from above, the hidden property &lt;code&gt;__proto__&lt;/code&gt; is assigned the reference to the prototype property on the function being invoked&lt;/li&gt;
&lt;li&gt;It returns the object stored at the &lt;code&gt;this&lt;/code&gt; property as long as no object is returned from the function being invoked (h/t to &lt;a href="https://dev.to/tbroyer"&gt;Thomas Broyer&lt;/a&gt; for clarifying this point)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We would have to do all that stuff manually, but the &lt;code&gt;new&lt;/code&gt; keyword &lt;strong&gt;does&lt;/strong&gt; all of that for us.&lt;/p&gt;

&lt;p&gt;Finally, let's take a look at it in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;createCar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;speed&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;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;color&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;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;speed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;createCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;go&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I prefer to drive &lt;/span&gt;&lt;span class="dl"&gt;'&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;speed&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;createCar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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;fast&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;myCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;go&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// will log 'I prefer to drive fast.' to the console&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myCar is&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;myCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// will log 'myCar is red' to the console&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>syntax</category>
      <category>engineering</category>
    </item>
  </channel>
</rss>
