<?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: Wendy</title>
    <description>The latest articles on DEV Community by Wendy (@kudacoder).</description>
    <link>https://dev.to/kudacoder</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%2F71134%2F62ec26c6-c828-4578-b161-476aaf0d1920.jpeg</url>
      <title>DEV Community: Wendy</title>
      <link>https://dev.to/kudacoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kudacoder"/>
    <language>en</language>
    <item>
      <title>Scouting Javascript Classes</title>
      <dc:creator>Wendy</dc:creator>
      <pubDate>Sat, 23 Mar 2019 20:37:25 +0000</pubDate>
      <link>https://dev.to/kudacoder/scouting-javascript-classes-o68</link>
      <guid>https://dev.to/kudacoder/scouting-javascript-classes-o68</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2F4f12rsK%2FScouting-Class.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2F4f12rsK%2FScouting-Class.png" alt="Scouting Javascript Classes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inspired by a conversation I heard yesterday I wanted to give a clear explaination of object-oriented JavaScript and &lt;strong&gt;&lt;em&gt;classes&lt;/em&gt;&lt;/strong&gt;. The first "space-time" key to understand classes is to understand &lt;strong&gt;&lt;em&gt;object literals&lt;/em&gt;&lt;/strong&gt;. You may be aware that code can be used to describe both abstract and real world objects. Object literals are a way of encapsulating data in a neat single package through key-value pairs of &lt;strong&gt;&lt;em&gt;properties&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;methods&lt;/em&gt;&lt;/strong&gt;. Since enhanced object literals have come into play with ES2015, it is even easier to create them. &lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FVSBbrGz%2Flunaandartemis.jpg%23feature" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FVSBbrGz%2Flunaandartemis.jpg%23feature" alt="Luna and Artemis"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, this is going to sound crazy, but the overheard conversation was between two cats talking about a magical heroine. Let's see if I can describe her as an object literal. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sailorMoon = {
  humanName: 'Usagi',
  nickName: 'Meatball Head',
  birthday: 'June 30',
  age: 14,
  favoriteAnimal: 'Rabbit',
  favoriteColor: 'White',
  favoriteFood: 'Ice Cream',
  strength: 'Loyalty',
  weakness: 'Thunder',
  phrase: function() {
    console.log('Moon Prism Power Makeup!')
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Notice that our heroine's object literal is arranged within curly brackets and key-value pairs are separated with a comma. All of the key-value pairs are &lt;strong&gt;&lt;em&gt;properties&lt;/em&gt;&lt;/strong&gt; of &lt;em&gt;const sailorMoon&lt;/em&gt;. The last pairing is called a &lt;strong&gt;&lt;em&gt;method&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
We can access different properties or methods of Sailor Moon using &lt;em&gt;dot notation&lt;/em&gt; like so:&lt;strong&gt;&lt;em&gt;objectName.propertyKey&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(sailorMoon.nickName);
    'Meatball Head'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FrGYJDQ0%2FSenshi-Sailor-Moon.jpg%23feature" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FrGYJDQ0%2FSenshi-Sailor-Moon.jpg%23feature" alt="Sailor Scouts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Class Constructor
&lt;/h3&gt;

&lt;p&gt;Our Sailor Moon object literal is all well and good, but imagine my astonishment on learning there were four other magical Sailor Scouts! There must be a quicker way to build our Sailor Senshi team than writing out the same object literal keys every time a new one magically makeups! Thanks to ES2015, yes there is! &lt;strong&gt;&lt;em&gt;Classes&lt;/em&gt;&lt;/strong&gt; in JavaScript are lovely syntatical sugar waving a moon crescent wand over object creation and inheritance issues.&lt;/p&gt;

&lt;p&gt;With &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt;, we can make a Sailor Scout blueprint and churn out four, five, even nine scouts with a few lines of code. &lt;strong&gt;Moon Cosmic Dream Action!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FNpWmxV3%2FOuter-Senshi.jpg%23feature" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FNpWmxV3%2FOuter-Senshi.jpg%23feature" alt="Outer Sailor Scouts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's start with declaring our &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt;. Notice the name of the &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt; is capitalized. This is common convention. Inside of our &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt;, we will add a &lt;strong&gt;&lt;em&gt;constructor&lt;/em&gt;&lt;/strong&gt; method. The &lt;em&gt;constructor&lt;/em&gt; is like a factory that will make all of the property values we request. We request these &lt;em&gt;parameters&lt;/em&gt; by placing them in the &lt;em&gt;()&lt;/em&gt;. So, what do you think a sailor scout needs? &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SailorScout {
  constructor(scoutName, humanName, birthday, element, favoriteColor, strength, weakness) {
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So we have our &lt;em&gt;constructor&lt;/em&gt; and request list (&lt;em&gt;parameters&lt;/em&gt;). Let's place some object &lt;em&gt;properties&lt;/em&gt; in the &lt;em&gt;constructor&lt;/em&gt; and link them to our &lt;em&gt;parameters&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;We need to use dot notation and the keyword &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt;. In this case &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; refers to the object being created. It is linked to a &lt;em&gt;property&lt;/em&gt; using dot notatation and set equal to a &lt;em&gt;parameter&lt;/em&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SailorScout {
  constructor(scoutName, humanName, birthday, element, favoriteColor, strength, weakness) {
    this.scoutName = scoutName;
    this.humanName = humanName;
    this.birthday =  birthday;
    this.element = element;
    this.favoriteColor = favoriteColor;
    this.strength = strength;
    this.weakness = weakness;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now we are ready to &lt;strong&gt;&lt;em&gt;instantiate&lt;/em&gt;&lt;/strong&gt; our Sailor Scout! &lt;em&gt;Instantiate&lt;/em&gt; means to create. First we will declare a variable to hold the object. We set the variable equal to the keyword &lt;em&gt;new&lt;/em&gt; and the &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt; name, then we pass in our Sailor Scout's values as &lt;em&gt;parameters&lt;/em&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sailorMercury = new SailorScout('Sailor Mercury', 'Ami', 'September 10', 'Water', 'Blue', 'Intelligence', 
'Shyness')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now if we &lt;strong&gt;&lt;em&gt;console.log(sailorMercury)&lt;/em&gt;&lt;/strong&gt; we should see all of Sailor Mercury's properties and values!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SailorScout {scoutName: "Sailor Mercury", humanName: "Ami", birthday: "September 10", element: "Water", favoriteColor: "Blue", strength: "Intelligence", weakness: "shyness"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Adding Methods to Our Class
&lt;/h3&gt;

&lt;p&gt;Sailor Scouts need to do something magical so we will need to add some &lt;em&gt;methods&lt;/em&gt; to our Sailor Scout &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt;. This is a similar process to adding a &lt;em&gt;method&lt;/em&gt; to our Sailor Moon object literal.  First, we want to add one more &lt;em&gt;property&lt;/em&gt; to our Sailor Scout: &lt;strong&gt;&lt;em&gt;magicalPhrase&lt;/em&gt;&lt;/strong&gt;. Then we will use that &lt;em&gt;property&lt;/em&gt; in a &lt;em&gt;method&lt;/em&gt; we will name &lt;strong&gt;&lt;em&gt;cast&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SailorScout {
  constructor(scoutName, humanName, birthday, element, favoriteColor, strength, weakness, magicalPhrase) {
    this.scoutName = scoutName;
    this.humanName = humanName;
    this.birthday =  birthday;
    this.element = element;
    this.favoriteColor = favoriteColor;
    this.strength = strength;
    this.weakness = weakness;
    this.magicalPhrase = magicalPhrase;
  }

  cast() {
    console.log(this.magicalPhrase);
  }

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

&lt;/div&gt;

&lt;p&gt;Let's see this in action:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const sailorMars = new SailorScout( "Sailor Mars", "Rei",  "April 17th", "Fire", "Red", "Psychic Abilities","Stubborness", "Mars Fire Ignite!")

  const sailorJupiter = new SailorScout("Sailor Jupiter", "Makoto", "December 5", "Thunder", "Green", "Physical Strength", "Impulsivity","Jupiter Thunder Crash!")

  sailorMars.cast();
  sailorJupiter.cast();

  Mars Fire Ignite!
  Jupiter Thunder Crash!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2F9HFcdvH%2Fsailor-mars-fire.jpg%23feature" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2F9HFcdvH%2Fsailor-mars-fire.jpg%23feature" alt="Mars Fire Ignite!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Congrats! You can now quickly create your own army of Sailor Senshi!!&lt;/strong&gt;&lt;br&gt;
Next time we will upgrade our Sailor Scouts with 'getters' and 'setters'!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>classes</category>
      <category>beginnertutorial</category>
    </item>
  </channel>
</rss>
