<?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: JosephKabemba</title>
    <description>The latest articles on DEV Community by JosephKabemba (@josephkabemba).</description>
    <link>https://dev.to/josephkabemba</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%2F540448%2F1d2096e1-9917-4e92-b734-56bb89853f40.png</url>
      <title>DEV Community: JosephKabemba</title>
      <link>https://dev.to/josephkabemba</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/josephkabemba"/>
    <language>en</language>
    <item>
      <title>JavaScript OOP is weird</title>
      <dc:creator>JosephKabemba</dc:creator>
      <pubDate>Sat, 06 Nov 2021 16:57:14 +0000</pubDate>
      <link>https://dev.to/josephkabemba/javascript-oop-is-weird-4h9j</link>
      <guid>https://dev.to/josephkabemba/javascript-oop-is-weird-4h9j</guid>
      <description>&lt;p&gt;When I discovered Object-Oriented Programming in JavaScript, I was confused. At first, It seemed straightforward. Creating a class was pretty much like Java.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student { 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But right after this declaration, a lot of questions popped into my mind. I've realized something was missing: the access modifier public before class. So, I added it. Unfortunately, my code editor didn't like it. public was highlighted. When I hovered over it, I got this message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The 'public' modifier can only be used in TypeScript files.ts(8009)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I was like "What ! I'm coding in JavaScript. Why Are you talking about Typescript ?". After a few searches on Google, I discovered JavaScript did not have the public keyword in its vocabulary. It was somewhat implicit.&lt;/p&gt;

&lt;p&gt;I continued coding. I wrote my first constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student {  

   constructor(name, faculty){   
      this.name = name;   
      this.faculty = faculty;   
  }
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Coming from a Java background and having done a few C++, it was weird. The constructor did not have the same name as the class and I'd initialized the instance properties name and faculty before declaring them upfront. I found out later that class fields existed. So, I could rewrite this code, the Java or C++ way. This put my mind at ease.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student {   
     name;   
     faculty;   

     constructor(name, faculty){   
       this.name = name;   
       this.faculty = faculty;   
     } 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nonetheless, I wasn't quite satisfied. I was wondering about making my class fields private to comply with OOP Encapsulation Principle that encourages to restrain direct access to class elements such as fields, properties or methods. As for public, the private keyword doesn't exist in JS world. So, I told myself at the moment: "How come this language is so limited ?".&lt;/p&gt;

&lt;p&gt;I was too eager to criticize until I found the #. All you have to do to make a property or method private is to put right before it the harsh (#) symbol.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import moment from "moment";

class Student {
  #name;
  #faculty;
  #birthDate;

  constructor(name, faculty, birthdate){
   this.#name = name;
   this.#faculty = faculty;
   this.#birthDate= birthDate;
  }

  #computeAge() {
    return moment(this.#birthDate, "YYYY-MM-DD").fromNow();
  }

  getAge() {
    return this.#computeAge();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OOP in JavaScript is not canonical. You will have a lot of surprises if you're coming from a pure Object Oriented Programming language. Consider migrating progressively to Typescript, which is a superset of JavaScript, to have less headache.&lt;/p&gt;




&lt;p&gt;If you enjoyed reading this post, we'll appreciate it if you could recommend it and share it with your friends. If you don't have ones, then just follow us, we'll be your friend.&lt;/p&gt;

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