<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Josephkabemba on Medium]]></title>
        <description><![CDATA[Stories by Josephkabemba on Medium]]></description>
        <link>https://medium.com/@josephkabemba?source=rss-59aa68436ebc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UxJ1-ZJTa62PQ8S4EatBag.png</url>
            <title>Stories by Josephkabemba on Medium</title>
            <link>https://medium.com/@josephkabemba?source=rss-59aa68436ebc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 15:24:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@josephkabemba/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to create Typed and Fixed-size Arrays in JavaScript]]></title>
            <link>https://medium.com/@josephkabemba/how-to-create-typed-and-fixed-size-arrays-in-javascript-f4f2fba28961?source=rss-59aa68436ebc------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4f2fba28961</guid>
            <dc:creator><![CDATA[Josephkabemba]]></dc:creator>
            <pubDate>Sun, 31 Oct 2021 02:33:14 GMT</pubDate>
            <atom:updated>2021-10-31T02:33:14.983Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*4tsdlIW7RqJOwOFNBg02sQ.png" /></figure><p>JavaScript is known for its flexibility. It’s a dynamically typed language. You don’t need and even can’t, if you wanted to, tell it beforehand explicitly the type of your variable or the return type of your function.</p><pre><strong>let </strong>birthDate = &#39;2018-04-01&#39;;</pre><pre><strong>function </strong>computeAge(birthDate){<br>  <strong>return </strong>moment(this.#birthDate, &#39;YYYY-MM-DD&#39;).fromNow();<br>}</pre><h3>What about Arrays?</h3><p>For arrays, it’s another story. Traditional array declaration is made like this:</p><pre><strong>let </strong>numbers = [1, 2, 3, 4, 5];</pre><pre><strong>let </strong>months = [&#39;January&#39;,&#39;February&#39;,&#39;March&#39;,&#39;April&#39;,&#39;May&#39;,&#39;June&#39;,&#39;July&#39;,&#39;August&#39;,&#39;September&#39;,&#39;October&#39;,&#39;November&#39;,&#39;December&#39;];</pre><p>Note that you’re not forced to have the same type of elements in your arrays, like in the example above. We could’ve written something like this:</p><pre><strong>let </strong>numbers = [1, 2.6, true , &quot;Congo&quot;];</pre><p>That’s totally fine. Javascript won’t complain. But What if you want your array to be filled solely by integers. It doesn’t make sense to have a list of numbers that contain strings or boolean values. How do you enforce that?</p><h3>Restrain the type of array items</h3><p>This is where the Typed Arrays come into play. If you want your array to be filled just with 8 bits, 16 bits, 32 bits integers, or only positive numbers, there are a bunch of typed arrays you can use for each purpose.</p><ul><li><strong>Int8Array: </strong>-128 to 127</li><li><strong>Uint8Array:</strong>0 to255</li><li><strong>Int16Array: </strong>-32768 to 32767</li><li><strong>Uint16Array: </strong>0 to 65535</li><li><strong>Int32Array: </strong>-2147483648 to 2147483647</li><li><strong>Uint32Array: </strong>0 to 4294967295</li><li><strong>Float32Array: </strong>1.2x10^-38 à 3.4x10³⁸</li><li><strong>Float64Array: </strong>5.0x10^-324 à 1.8x10³⁰⁸</li></ul><p>To declare a typed array, you will write among other ways:</p><pre><strong>let </strong>numbers = <strong>new Int32Array</strong>([15, 25, 78]);</pre><p>For further reading about typed arrays, here is a link from Mozilla: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays</a></p><h3>Making an array static: How to fix the size of an array</h3><p>Something interesting about Typed Arrays is that, unlike traditional ones, they are not shipped with <strong><em>push </em></strong>and <strong><em>pop </em></strong>methods. This makes them fixed by default.</p><p>So, invoking the <em>push </em>method on these kinds of arrays will result in an error.</p><pre><strong>TypeError</strong>: numbers.<strong>push </strong>is not a function</pre><h4><em>How do you make an untyped traditional array fixed?</em></h4><p>Simple. You just pass it to <strong><em>Object.seal()</em></strong> method.</p><pre><strong>const </strong>numbers = [15, 78, 19, 23, 41];<br><strong>Object</strong>.<strong>seal</strong>(numbers);</pre><p>After sealing the array, any call of push or pop methods will fill your terminal with an error message to remind either that your array is not extensible or shrinkable.</p><pre><strong>TypeError</strong>: Cannot add property 5, <strong><em>object is not extensible</em></strong></pre><h4>Object.seal() vs Object.freeze()</h4><p>One of the alternative to <em>Object.seal()</em> is <strong><em>Object.freeze()</em></strong>.<strong><em> </em></strong>The main difference between the two is that the latter, in addition to making the array unresizable, prevents updating the values of items. For instance, in the case of an array of months, we can’t imagine the number of months to change any time soon or to be renamed. We will, in this case, freeze it altogether. We’re guaranteed afterward nothing will change.</p><pre><strong>let </strong>months = [&#39;January&#39;,&#39;February&#39;,&#39;March&#39;,&#39;April&#39;,&#39;May&#39;,&#39;June&#39;,&#39;July&#39;,&#39;August&#39;,&#39;September&#39;,&#39;October&#39;,&#39;November&#39;,&#39;December&#39;];</pre><pre><strong>Object</strong>.<strong>freeze</strong>(months);</pre><pre>months[0] = &#39;Joseph&#39;; /* this will result in the error:  <br><strong><em>TypeError: Cannot assign to read only property</em></strong> */</pre><p>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.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4f2fba28961" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript OOP is weird]]></title>
            <link>https://medium.com/@josephkabemba/javascript-oop-is-weird-ed910824b64f?source=rss-59aa68436ebc------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed910824b64f</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[oops-concepts]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Josephkabemba]]></dc:creator>
            <pubDate>Sat, 30 Oct 2021 03:26:24 GMT</pubDate>
            <atom:updated>2021-10-31T12:22:54.110Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8KVw54e5929GgMkma-mIlA.jpeg" /></figure><p>When I discovered Object-Oriented Programming in JavaScript, I was confused. At first, It seemed straightforward. Creating a class was pretty much like Java.</p><pre><strong>class </strong>Student { </pre><pre>}</pre><p>But right after this declaration, a lot of questions popped into my mind. I’ve realized something was missing: the access modifier <em>public </em>before <em>class. </em>So, I added it. Unfortunately, my code editor didn’t like it. <em>public </em>was highlighted. When I hovered over it, I got this message.</p><pre>The &#39;<strong>public</strong>&#39; modifier can only be used in TypeScript files.ts(8009)</pre><p>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 <em>public</em> keyword in its vocabulary. It was somewhat implicit.</p><p>I continued coding. I wrote my first constructor.</p><pre><strong>class </strong>Student {  <br> <br>   <strong>constructor</strong>(name, faculty){   <br>      this.name = name;   <br>      this.faculty = faculty;   <br>  }<br>} </pre><p>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 <em>name </em>and <em>faculty </em>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.</p><pre><strong>class </strong>Student {   <br>     name;   <br>     faculty;   <br>     <br>     <strong>constructor</strong>(name, faculty){   <br>       this.name = name;   <br>       this.faculty = faculty;   <br>     } <br>}</pre><p>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 <em>public</em>, the <strong><em>private</em></strong> keyword doesn’t exist in JS world. So, I told myself at the moment: “How come this language is so limited ?”.</p><p>I was too eager to criticize until I found the <strong>#.</strong> All you have to do to make a property or method private is to put right before it the harsh (#) symbol.</p><pre><strong>import</strong> moment <strong>from</strong> &quot;moment&quot;;</pre><pre><strong>class </strong>Student {</pre><pre>  #name;<br>  #faculty;<br>  #birthDate;</pre><pre>  <strong>constructor</strong>(name, faculty, birthdate){<br>   this.#name = name;<br>   this.#faculty = faculty;<br>   this.#birthDate= birthDate;<br>  }<br> <br>  #computeAge() {<br>    return moment(this.#birthDate, &quot;YYYY-MM-DD&quot;).fromNow();<br>  }</pre><pre>  getAge() {<br>    return this.#computeAge();<br>  }<br>}</pre><p>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.</p><p>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.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed910824b64f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>