<?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: vazsonyidl</title>
    <description>The latest articles on DEV Community by vazsonyidl (@vazsonyidl).</description>
    <link>https://dev.to/vazsonyidl</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%2F341023%2F9e915181-bb05-4927-8d49-91fd1c687b9f.png</url>
      <title>DEV Community: vazsonyidl</title>
      <link>https://dev.to/vazsonyidl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vazsonyidl"/>
    <language>en</language>
    <item>
      <title>Goodbye padding?</title>
      <dc:creator>vazsonyidl</dc:creator>
      <pubDate>Tue, 12 Apr 2022 15:20:36 +0000</pubDate>
      <link>https://dev.to/vazsonyidl/goodbye-padding-8n6</link>
      <guid>https://dev.to/vazsonyidl/goodbye-padding-8n6</guid>
      <description>&lt;p&gt;Come on, it is twenty-twentysomething now, we really should know everything about accessibility, standards, usability, etc.. But how much time do we &lt;em&gt;usually&lt;/em&gt; spend with styling, and how much effort is taken away with the N+1 JavaScript framework released every Monday? &lt;/p&gt;

&lt;h2&gt;
  
  
  It is a pain..
&lt;/h2&gt;

&lt;p&gt;...I read it on several pages, hear it from the community. That is why Tailwind CSS shines so bright, but rather learning all the tags of a CSS framework, people can actually do their own styling. So jump into our today's pick, the magical padding.&lt;/p&gt;

&lt;h2&gt;
  
  
  The box model
&lt;/h2&gt;

&lt;p&gt;I hope that everyone is familiar already with the box model, but let me attach a picture and the &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/The_box_model"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8szgdQvk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wfz9cujilbccus1pxmp8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8szgdQvk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wfz9cujilbccus1pxmp8.png" alt="The box model" width="880" height="523"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In CSS, usually everything has a box around it and it can be either &lt;em&gt;inline&lt;/em&gt; or &lt;em&gt;block&lt;/em&gt;. When the browsers parses your HTML, it will logically organise these super fancy squares that a 2 year old can draw already.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give me some space
&lt;/h2&gt;

&lt;p&gt;So you want to give your element a little free-space, not to look that &lt;em&gt;bad&lt;/em&gt;. Okay, there is no good or bad, so just to add some spacing around it. I mean, with padding, your elements still be so close to each other like you to your commuting-fellows at 5 p.m. on the underground, but at least your arm will be &lt;em&gt;8px&lt;/em&gt; away from your coat sleeve on the inside. That is padding. Hope it helps during your next interview. :) &lt;/p&gt;

&lt;h2&gt;
  
  
  The problem arises when..
&lt;/h2&gt;

&lt;p&gt;..when you only think about yourself. &lt;strong&gt;Really&lt;/strong&gt;. Do you read from top-to-bottom/left-to-right? Awesome, than you are actually part of ~70% of the people from this point-of-view, but you miss out a few people, who reads from right-to-left/top-to-bottom, etc. &lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Okay, enough from anecdotes, jump into the code. The following &lt;br&gt;
code shows what I was trying to point out.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in the first paragraph, it works as intended&lt;/li&gt;
&lt;li&gt;on the other hand, the second (if the direction is &lt;em&gt;rtl&lt;/em&gt;) we can immediately see the issue.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The padding is there, but.. :) &lt;/p&gt;

&lt;p&gt;You can say that use &lt;code&gt;padding: 0 8px&lt;/code&gt; instead, but come-on...&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/vazsonyidl/embed/bGajBYX?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing mode
&lt;/h2&gt;

&lt;p&gt;You may come up with the idea, that instead of &lt;em&gt;rotating&lt;/em&gt; the title of your Y-axis on a diagram, you end up using the less-known &lt;em&gt;writing-mode&lt;/em&gt; CSS property. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode"&gt;Docs here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;In this case, what happens? With padding-left (which you originally set for to give some extra space for the 'beginning' of your element) is actually on the left-hand side now. &lt;/p&gt;

&lt;p&gt;Which is the bottom of your text, and the top is the left. &lt;/p&gt;

&lt;p&gt;What?! Let me demonstrate:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/vazsonyidl/embed/MWrBNQL?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  There is even more
&lt;/h3&gt;

&lt;p&gt;Things can get to be really complicated when you throw in some &lt;code&gt;text-orientation&lt;/code&gt; as well - apart from &lt;code&gt;writing-mode&lt;/code&gt;. I do not want to really go into the nitty-gritty details of all of these, MDN documentation is always there for edge cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Rather than sticking to the well-know and easy-to-use &lt;code&gt;padding-left/right/top/bottom&lt;/code&gt; take into consideration, that accessibility and usability is not just some buzz-words, but actual user-experience depends on you! :) &lt;/p&gt;

&lt;p&gt;There is always room for improvement, some minor one is &lt;code&gt;padding-inline&lt;/code&gt;, but it's big brother &lt;code&gt;padding-block&lt;/code&gt; is worth a mention as well.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed this article and I was able to show you something new. If you have anything to add, I welcome every comment.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to boost your productivity</title>
      <dc:creator>vazsonyidl</dc:creator>
      <pubDate>Sun, 06 Mar 2022 11:58:03 +0000</pubDate>
      <link>https://dev.to/vazsonyidl/dont-run-for-3rd-party-libraries-51i8</link>
      <guid>https://dev.to/vazsonyidl/dont-run-for-3rd-party-libraries-51i8</guid>
      <description>&lt;p&gt;In the next couple of lines I would like to share you my experience from the latest feature developed at the current company.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;!&lt;/strong&gt; You should not implement &lt;strong&gt;authentication, cryptographic&lt;/strong&gt; solutions from scratch. In those cases, please rely on third party solutions, unless you &lt;em&gt;really know what you are doing&lt;/em&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  The eggs hatched
&lt;/h2&gt;

&lt;p&gt;When I started to write this post, it would have contained only the first part. Thankfully forgot to post - because now, after a few weeks - I have another first-hand experience to share with you! (Although I will do that in another post soon :) )&lt;/p&gt;

&lt;h3&gt;
  
  
  The scene - for the first occasion
&lt;/h3&gt;

&lt;p&gt;So our designer dreamed a new page for the latest feature. It is a custom graph displaying several different information about booking tables for different periods and for varying length in dates. The main part of the page looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ki7m1m9G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekvo0ie4m9eckojo8h62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ki7m1m9G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekvo0ie4m9eckojo8h62.png" alt="Image description" width="880" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  The difficulties
&lt;/h4&gt;

&lt;p&gt;It contains the two most difficult (or rather tricky) thing to deal with in JavaScript (in my opinion): dates, and a graph/chart.&lt;/p&gt;

&lt;h4&gt;
  
  
  The possibilities
&lt;/h4&gt;

&lt;p&gt;Picking some third party solutions for the project usually includes these two reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Having the pressure from the product side&lt;/li&gt;
&lt;li&gt;Try not to reinvent the wheel&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With this on the table, every team tries to deliver the feature as fast as possible - within an achievable time period.&lt;/p&gt;

&lt;h4&gt;
  
  
  The path
&lt;/h4&gt;

&lt;p&gt;I started to look for various third-party libraries, like &lt;br&gt;
&lt;a href="https://d3js.org"&gt;d3js&lt;/a&gt; or &lt;a href="https://github.com/recharts/recharts"&gt;recharts&lt;/a&gt;. Oh, I hope I mentioned, we use React.js.&lt;/p&gt;

&lt;p&gt;Everything was like a roller-coaster. One day it looked like there is a possibility to implement all the details with some compromise, but soon I realised that it will not be the solution.&lt;/p&gt;

&lt;p&gt;During a bath, a new idea started to take shape, that maybe I should solve it with plain HTML elements. After all, a diagram is a &lt;em&gt;table&lt;/em&gt;, where the available spaces are the Y axis, and the time period is the X axis. Each cell contains a &lt;em&gt;filled status&lt;/em&gt;, if that particular space is sold or not.&lt;/p&gt;

&lt;h4&gt;
  
  
  The surprise
&lt;/h4&gt;

&lt;p&gt;To my greatest surprise, it was much quicker to solve the problem this way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You do not have to concentrate on the API of the library&lt;/li&gt;
&lt;li&gt;Your possibilities are not limited by the API&lt;/li&gt;
&lt;li&gt;You can spend more time to completely match your core business logic (even the edge cases)&lt;/li&gt;
&lt;li&gt;React.js is your friend with &lt;a href="https://reactjs.org/docs/reconciliation.html"&gt;Reconciliation&lt;/a&gt; to optimise rerendering (You can search for more years in our case)&lt;/li&gt;
&lt;li&gt;You are free to jump between major versions - one dependency less to track the compatibility with.&lt;/li&gt;
&lt;li&gt;Smaller bundle size&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The cons:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I think there are a few, and the most important one was wasting ~1 week with finding the 3rd party lib that matched our use-case.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have to implement the styling by yourself. (Is it even a con? CSS is awesome and easy!)&lt;/li&gt;
&lt;li&gt;Third party libraries may provide some additional features (but does not cover your main requirement, so..)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Footnote
&lt;/h3&gt;

&lt;p&gt;I do not want to say that using third part libraries are bad. There are awesome ones, and each of those serves a purpose.&lt;/p&gt;

&lt;p&gt;My suggestion is: &lt;strong&gt;always look for possible third party options as well as think about how to solve it with plain HTML/TS/CSS.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The more experience you gain, the less time you can make the current best decision.&lt;/p&gt;

&lt;p&gt;And remember: &lt;em&gt;the junior programmer knows what to use, the senior knows what &lt;strong&gt;not&lt;/strong&gt; to use&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Selection sort</title>
      <dc:creator>vazsonyidl</dc:creator>
      <pubDate>Sun, 07 Nov 2021 18:02:22 +0000</pubDate>
      <link>https://dev.to/vazsonyidl/selection-sort-4dgc</link>
      <guid>https://dev.to/vazsonyidl/selection-sort-4dgc</guid>
      <description>&lt;p&gt;Cover photo from: &lt;a href="https://unsplash.com/photos/cPDH2ChdBps" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;This post is a part of a sorting series, where we go over each sorting methods from bubble sort to radix sort, and I will give you some simple explanation. The importance of sorting can be measured by the success of Google search engine vs other search engines. At the end of the day, it gives you the most useful results - based on its well designed sorting algo.&lt;/p&gt;

&lt;h3&gt;
  
  
  Selection sort
&lt;/h3&gt;

&lt;p&gt;Selection sort basically a very easy method, and can be imagined and implemented in the same way. The main logic is the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Take an unsorted array of elements&lt;/li&gt;
&lt;li&gt;Create a new array - for ex. starting from the left, initially [] - this will be your sorted sublist&lt;/li&gt;
&lt;li&gt;Your input will be your second, unsorted sublist&lt;/li&gt;
&lt;li&gt;Find the smallest - or largest - element of your unsorted sublist - depending on your preferences&lt;/li&gt;
&lt;li&gt;Insert that into your sorted sublist - at the end&lt;/li&gt;
&lt;li&gt;Move the boundaries&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
   Visual demo
&lt;/h4&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%2Fstackabuse.s3.amazonaws.com%2Fmedia%2Fselection-sort-in-javascript-1.gif" 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%2Fstackabuse.s3.amazonaws.com%2Fmedia%2Fselection-sort-in-javascript-1.gif" alt="Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance and complexity
&lt;/h3&gt;

&lt;p&gt;Sadly, this is an easy solution but it costs you some memory and computation power, and I will show you this through an example. &lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;p&gt;Let say, your starting element is in &lt;code&gt;const array = [ 4, 1, 3, 9];&lt;/code&gt;, in where the length of your array is &lt;code&gt;n = 4&lt;/code&gt;;&lt;/p&gt;

&lt;p&gt;You have to find the smallest element of this list, which requires &lt;strong&gt;n - 1&lt;/strong&gt; comparisons. Finding the next one requires &lt;strong&gt;n - 2&lt;/strong&gt; and so on. On large lists, this soon become &lt;em&gt;quadratic&lt;/em&gt; - known as &lt;em&gt;O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  When to use?
&lt;/h4&gt;

&lt;p&gt;If you have small lists - and you are aware and you manage the length of your input, you can use this. In this situation, it can be helpful because of its simplicity.&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementation
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function selectionSort(array) {

// break the reference
  const elements = [...array];
  for (let i = 0; i &amp;lt; elements.length; i++) {

// assume that the 'first` is the smallest
    let smallestIndex = i;

// find the smallest index
    for(let j = i + 1; j &amp;lt; elements.length; j++) if(elements[j] &amp;lt; elements[smallestIndex]) smallestIndex = j;

// if the index is not the same, swap the two numbers
    if(smallestIndex !== i) [elements[i], elements[smallestIndex]] = [elements[smallestIndex], elements[i]]
  }

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Demo
&lt;/h4&gt;

&lt;p&gt;You can check out this demo page, which is fancy enough to give you a nice understanding.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://visualgo.net/en/sorting" rel="noopener noreferrer"&gt;Visualgo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks four your time :)&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>algorithms</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Bubble-sort</title>
      <dc:creator>vazsonyidl</dc:creator>
      <pubDate>Thu, 07 Oct 2021 17:30:57 +0000</pubDate>
      <link>https://dev.to/vazsonyidl/bubble-sort-4g51</link>
      <guid>https://dev.to/vazsonyidl/bubble-sort-4g51</guid>
      <description>&lt;p&gt;Cover image from: &lt;a href="https://unsplash.com/photos/IBo9Ehrzcfs"&gt;Unsplash  - Kai Dahms&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;This is going to be a series about different sorting algorithms, some explanation and a quick demo with solution in JavaScript. &lt;/p&gt;

&lt;h4&gt;
  
  
  Bubble sort
&lt;/h4&gt;

&lt;p&gt;First of all, let start with the most basic one - bubble sort.&lt;br&gt;
The logic behind bubble sort is the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;starting from the beginning, compare two adjacent elements&lt;/li&gt;
&lt;li&gt;if the previous one is bigger than the following one, swap those two&lt;/li&gt;
&lt;li&gt;repeat until there is no element left in the array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is just one iteration, which guarantees that the biggest element is at the end of the array.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;repeat this process for every element in the array&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Complexity
&lt;/h4&gt;

&lt;p&gt;As you may see, there are several different outcomes, and based on the number of the elements to compare, things quickly can get out of control. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best case scenario&lt;/strong&gt;: The elements are ordered &amp;gt; we will do &lt;strong&gt;O(n)&lt;/strong&gt; comparisons. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Worst case scenario&lt;/strong&gt;: The elements are reverse ordered &amp;gt; we will do &lt;strong&gt;O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/strong&gt; comparison. It does not look like a problem for 10 elements, but for 1000, there will be a lot of zero after that first leading one. :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Average scenario&lt;/strong&gt;: Sadly, the average scenario has the same time complexity as the worst one, which still will be &lt;strong&gt;O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/strong&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  Usage
&lt;/h4&gt;

&lt;p&gt;I think bubble sort is not so problematic, because of its ease to understand. Use it wisely, and use it for small amount of elements. There is nothing wrong with it - up to a minimal amount of elements.&lt;/p&gt;
&lt;h4&gt;
  
  
  Implementation
&lt;/h4&gt;

&lt;p&gt;Of course, there are a lot of way to implement this, but I will leave mine here for anyone who is interested in. I will link the GitHub repo for this as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function bubbleSort(_array) {
  const array = _array;
  for (let i = 0; i &amp;lt; array.length - 1; i++)
    for (let j = 0; j &amp;lt; array.length - i; j++)
      if (array[j] &amp;gt; array[j + 1]) [array[j], array[j + 1]] = [array[j + 1], array[j]];

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

&lt;/div&gt;



&lt;p&gt;Some sneak peak:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the easiest way to swap two variables is [a, b] = [b, a] &amp;gt; you do not need a &lt;code&gt;tmp&lt;/code&gt; one then&lt;/li&gt;
&lt;li&gt;you do not have to loop the array until the end every iteration &amp;gt; if the greatest is already at the end (and the nth greatest .. so on) leave that alone&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Reference
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://github.com/vazsonyidl/sorting-algorithms/blob/main/src/bubble-sort.js"&gt;Repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>algorithms</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Angular DI in action - series</title>
      <dc:creator>vazsonyidl</dc:creator>
      <pubDate>Sat, 31 Jul 2021 12:38:36 +0000</pubDate>
      <link>https://dev.to/vazsonyidl/angular-di-in-action-series-3669</link>
      <guid>https://dev.to/vazsonyidl/angular-di-in-action-series-3669</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In Angular, you can use &lt;code&gt;services&lt;/code&gt; for several purposes. You can share state between components, share common functionalities, create plus one abstraction layer for some problem and so on. Services are just basic classes with the @Injectable() decorator. But how does this decorator actually works, what are the resolution rules that describes which service to use in a component? &lt;/p&gt;

&lt;p&gt;Let's dive into these topic. This will be a series with 4-6 parts, and this is the first one.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is DI anyway?
&lt;/h3&gt;

&lt;p&gt;Dependency Injection is a design pattern, where the initiated class request its dependencies from external sources instead of creating them by itself. With this, you can increase modularity, flexibility and reusability of your codebase.&lt;/p&gt;

&lt;h4&gt;
  
  
  Starting point
&lt;/h4&gt;

&lt;p&gt;Let's create a demo service for our starting point. It does not matter what this service can do, because we will discover how Angular handles dependency injection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from "@angular/core";

@Injectable()
export class DemoService {
  constructor() {}
}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Provide possibilities of a service
&lt;/h4&gt;

&lt;p&gt;So if you would like to use this service, what are your options to tell Angular &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I need this service, so inject this for me?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are two popular ways - as of my current experiences.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Providing it on root level with &lt;code&gt;providedIn: 'root'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Providing it on module level&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These two uses almost the same technique with a slight difference. But are these two is used because these are the best solutions, or there are some convenient reasons behind this? &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Angular modules never got destroy, so neither the services provided on them&lt;/strong&gt; &lt;sup&gt;1&lt;/sup&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It not obviously a problem, but good to know this. :)&lt;/p&gt;

&lt;h4&gt;
  
  
  Dependency lookup mechanism
&lt;/h4&gt;

&lt;p&gt;When you are providing a service, you are requesting that resource from the DI. Dependency injection is solved in the background for you by Angular, but it is worth to have a bit of knowledge how it works. &lt;/p&gt;

&lt;p&gt;First of all, there are three different hierarchical injectors (that you can configure):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;ModuleInjector: &lt;code&gt;@NgModule&lt;/code&gt; and &lt;code&gt;@Injectable&lt;/code&gt; with &lt;code&gt;providedIn&lt;/code&gt; - module based&lt;/li&gt;
&lt;li&gt;ElementInjector: empty by default, but sticked to a component&lt;/li&gt;
&lt;li&gt;root ModuleInjector: created when bootstrapping the application&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;and there are two more that you can not configure:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;NullInjector(): this is the top/lowest one&lt;/li&gt;
&lt;li&gt;PlatformModule's ModuleInjector() - provide special core things&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Visualised, it looks like the following - taken from the official docs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HXZ9Y5Q4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhdql7ovzrhsx2d22r95.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HXZ9Y5Q4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhdql7ovzrhsx2d22r95.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  ModuleInjector
&lt;/h4&gt;

&lt;p&gt;ModuleInjector is created with &lt;code&gt;providers&lt;/code&gt; and &lt;code&gt;import&lt;/code&gt; properties in any of the &lt;code&gt;@NgModule&lt;/code&gt;s. At the end, this is a flattening of all of your &lt;code&gt;providers&lt;/code&gt; of all the modules that can be reached by any of the &lt;code&gt;imports&lt;/code&gt;, recursively.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of it as a huge basket of providers - configured by you!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Child ModuleInjectors are created when you lazy load a new module.&lt;/p&gt;

&lt;h4&gt;
  
  
  ElementInjector
&lt;/h4&gt;

&lt;p&gt;Angular creates an ElementInjector for every DOM node implicitly. You can configure this injector with the &lt;code&gt;providers&lt;/code&gt; or &lt;code&gt;viewProviders&lt;/code&gt; property of a component decorator.&lt;/p&gt;

&lt;h4&gt;
  
  
  NullInjector
&lt;/h4&gt;

&lt;p&gt;This is the parent of every Injector in the hierarchy.&lt;br&gt;
If you reach this point - without any modifier - an error will be thrown for you. Yes, that well know error: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;NullInjectorError: No provider for XYZService&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is it important?
&lt;/h3&gt;

&lt;p&gt;So you may have asked yourself now: why is it important for us at all? I can tell you the reason: with understanding the base mechanism of injectors, we can dive a bit deeper to understand how Dependency Injector traverse up the hierarchical graph of this Injectors and how can we influence this lookup mechanism. &lt;/p&gt;

&lt;p&gt;With this, you can easily understand why you have a &lt;code&gt;NullInjectorError&lt;/code&gt;, why your components have a different instance of your service.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This may help you to debug faster and find the solution for your problem easier.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Stay tuned
&lt;/h4&gt;

&lt;p&gt;This was the first part of this series. In the following one, we will check the resolution rules of dependencies as well as the first resolution modifier.&lt;/p&gt;

&lt;p&gt;Have a nice day! :)&lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://github.com/angular/angular/issues/37095"&gt;1&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>RxJS 7 is released 🎉</title>
      <dc:creator>vazsonyidl</dc:creator>
      <pubDate>Thu, 06 May 2021 19:21:17 +0000</pubDate>
      <link>https://dev.to/vazsonyidl/rxjs-7-is-released-323a</link>
      <guid>https://dev.to/vazsonyidl/rxjs-7-is-released-323a</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;RxJS v7.0.0 was released just a few days ago, after a more-than-one-year long beta period. It is definitely the most used reactive extension library used for JavaScript with ~24 millions download per week.&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/rxjs" rel="noopener noreferrer"&gt;Npm page&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are no removal yet, but a lot of deprecations - that will be removed later - so it is recommended to allocate time for the RxJS update! &lt;/p&gt;
&lt;h2&gt;
  
  
  What's new
&lt;/h2&gt;

&lt;p&gt;It is not that much upgrade that was the previous major package updates, but there are several notable differences between version 6 and seven, let's take a closer look.&lt;/p&gt;
&lt;h4&gt;
  
  
  Smaller bundle size
&lt;/h4&gt;

&lt;p&gt;It is worth a point that the whole package had been walked through by the creators to check out where are possibilities to - at the end - reduce the bundle size. There were no major refactor with the app - as I saw and read.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmi59kjrqrru77jvegg97.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmi59kjrqrru77jvegg97.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Maybe the reduce size is not that outstanding, but every kB counts with slow network especially on mobile. You may see the reducing trend in the chart, it is a good way to go. :)&lt;/p&gt;

&lt;p&gt;You can check out the bundle size here, on &lt;a href="https://bundlephobia.com/result?p=rxjs@7.0.0" rel="noopener noreferrer"&gt;Bundlephobia&lt;/a&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  Latest TypeScript &amp;amp; better type interference
&lt;/h4&gt;

&lt;p&gt;RxJS uses the latest TypeScript (as of 2021.05.06) and also has some real improvements interfering different types. The limit, that around ~7/8 argument RxJS was not able to handle types no longer exists!&lt;/p&gt;

&lt;p&gt;There is another example for this, let's take a look at this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;of(new Date(), null, undefined)
  .pipe(filter(Boolean))
  .subscribe();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the type will be &lt;code&gt;Observable&amp;lt;Date&amp;gt;&lt;/code&gt;, but it was &lt;code&gt;Observable&amp;lt;undefined&amp;gt;&lt;/code&gt; in RxJS 6.&lt;/p&gt;

&lt;h4&gt;
  
  
  toPromise deprecation
&lt;/h4&gt;

&lt;p&gt;Maybe this is not relevant for someone but a lot of projects may be affected from this. &lt;code&gt;toPromise&lt;/code&gt; is deprecated in RxJS 7 and there are two new operators replacing this one, called &lt;code&gt;firstValueFrom&lt;/code&gt; and &lt;code&gt;lastValueFrom&lt;/code&gt;. It is a huge improvement for reliability for many codebases.&lt;/p&gt;

&lt;p&gt;AS the name indicates, &lt;code&gt;firstValueFrom&lt;/code&gt; resolves with the first value of a stream, and &lt;code&gt;lastValueFrom&lt;/code&gt; return with the last value from the observable stream. &lt;strong&gt;If no values emitted, an error is thrown.&lt;/strong&gt; Unlike toPromise, which resolves simply with undefined.&lt;/p&gt;

&lt;h4&gt;
  
  
  Operators renamed
&lt;/h4&gt;

&lt;p&gt;I remember we had a discussion with the team that: "wish the operators in RxJS could have more talkative names". It happened, so the following operators are renamed&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;combineLatest -&amp;gt; combineLatestWith&lt;/li&gt;
&lt;li&gt;merge -&amp;gt; mergeWith&lt;/li&gt;
&lt;li&gt;zip -&amp;gt; zipWith&lt;/li&gt;
&lt;li&gt;concat -&amp;gt; concatWith&lt;/li&gt;
&lt;li&gt;race -&amp;gt; raceWith&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Retry operator with resetOnSuccess
&lt;/h4&gt;

&lt;p&gt;Previously the retry operator's parameter was not reseted after successful attempts. Now there is a configuration option to indicate this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
retry({ count: 2, resetOnSuccess: true })
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Removing multiple callback options
&lt;/h4&gt;

&lt;p&gt;In RxJS 7 the multiple callback for done, error, complete had been removed from &lt;code&gt;tap&lt;/code&gt; and &lt;code&gt;subscribe&lt;/code&gt;. Now you need to pass an object for these configuration, just to force you to think it two times and make sure.&lt;/p&gt;

&lt;p&gt;Now, instead of this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;obs$.pipe(tap(
  data =&amp;gt; console.log(data),
  error =&amp;gt; console.log(error)
)).subscribe(
  data =&amp;gt; console.log(data),
  error =&amp;gt; console.log(error)
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you have to do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source$.pipe(tap(
  data =&amp;gt; console.log(data)
)).subscribe(
  {
    next:  data =&amp;gt; console.log(data), 
    error:  err =&amp;gt; console.log(err),
  }
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Faster
&lt;/h4&gt;

&lt;p&gt;According to some tweets and discussions, developers claims that RxJS 7 is faster. Still, it needs to stand the probe of time, but I think it will. :)&lt;/p&gt;

&lt;h5&gt;
  
  
  Footnote
&lt;/h5&gt;

&lt;p&gt;Of course there are several other updates in RxJS and shoutout to the developer team for releasing this package. You can read more about the update in details on the following links:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/6-to-7-change-summary.md" rel="noopener noreferrer"&gt;Change summary&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/volosoft/whats-new-in-rxjs-7-a11cc564c6c0" rel="noopener noreferrer"&gt;Medium article&lt;/a&gt;&lt;br&gt;
&lt;a href="https://indepth.dev/posts/1353/the-state-of-rxjs-rxjs-7-and-beyond" rel="noopener noreferrer"&gt;inDepth article&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>functional</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Mock recursive calls with Jest</title>
      <dc:creator>vazsonyidl</dc:creator>
      <pubDate>Wed, 28 Apr 2021 17:31:59 +0000</pubDate>
      <link>https://dev.to/vazsonyidl/mock-recursive-calls-with-jest-3k6a</link>
      <guid>https://dev.to/vazsonyidl/mock-recursive-calls-with-jest-3k6a</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;In the recent cadence there were some problems that had been solved with recursive function calls. It is not a problem at all, but what if you have to write unit test for these functions? Forget the what if, you have to write unit tests for your functions, of course! In the next few sections I would like to share my experience with you, and how you can solve this kind of problems.&lt;/p&gt;

&lt;p&gt;This post will show you how to solve this problem with Jest.&lt;/p&gt;

&lt;h4&gt;
  
  
  What this post is not about
&lt;/h4&gt;

&lt;p&gt;This post is not about those fancy stuffs like memoization, recursive function calls to solve fib(50) with the most optimal way, etc and testing these things. While these are great stuff, I have never met with the requirement to memoize a fibonacci call in production, so these tasks are good for your brain and for learning purposes, but now turn our attention to some real life issues.&lt;/p&gt;

&lt;p&gt;A bit nicer solution would be a WebSocket, but .. for the sake of demonstration. :) &lt;/p&gt;

&lt;h4&gt;
  
  
  Setting up the scene
&lt;/h4&gt;

&lt;p&gt;So, imagine you have to solve some pulling thing collaborating with you backend. For example, there is a generation process in progress and your task is to polling this endpoint as long as the required response has not come yet. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;The happy path is:&lt;/em&gt; your first call will succeed and you are on your way to make your user happy. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;The sad path is:&lt;/em&gt; the generation is in progress in the first (or second, ...) call and you have to pull files until you have a satisfying result.&lt;/p&gt;

&lt;h5&gt;
  
  
  Code example for this
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  private getChannelInfo(): Observable&amp;lt;string&amp;gt; {
    const channel_id = this.getChannelId();
    return this.getInfo&amp;lt;InfoResponse&amp;gt;(`/api/_info`, {
        params: {channel_id},
      })
      .pipe(
        delay(1000),
        switchMap(body =&amp;gt; body?.completed ? of(body) : this.getChannelInfo()),
      );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As long as the endpoint not responds with a satisfying result, specially with completed, we are fetching again with a one second delay. &lt;/p&gt;

&lt;h5&gt;
  
  
  The problem
&lt;/h5&gt;

&lt;p&gt;Okay, we set the scene, now we should write test for it. Okay, mock the return value for the getInfo function call. But what if we mocked that once? We should flush that mock, and provide another return value on the second call, for example.&lt;/p&gt;

&lt;h4&gt;
  
  
  The solution
&lt;/h4&gt;

&lt;p&gt;Maybe there are several hacky workarounds for this, I think Jest provides a really straightforward solution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('should pull channel info again', () =&amp;gt; {
// ... the rest of your setup
serviceUnderTest.getInfo = jest.fn().mockReturnValueOnce(of(null)).mockReturnValueOnce(of(mock));
// ... your assertions
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we can see in this example, when the first function calls happen for the &lt;code&gt;getInfo&lt;/code&gt; function, jest steps in and says&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;on the first call, I will return of(null)&lt;/li&gt;
&lt;li&gt;on the second call, I will return of(mock)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this chaining of mocks, you can provide multiple return values based on the times of call for your function. With this, you can easily test thoroughly your recursive logic in your service/components.&lt;/p&gt;

&lt;h5&gt;
  
  
  Addition
&lt;/h5&gt;

&lt;p&gt;There is an option to provide return value for any other calls for the function with the following set-up:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jest.fn().mockReturnValue([]).mockReturnValueOnce(of(null)).mockReturnValueOnce(of(mock));&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the first and seconds returns as described above&lt;/li&gt;
&lt;li&gt;any other function call will return with []&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope this give you a bit of interest with this topic, any comments and responses are welcomed :)&lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://jestjs.io/docs/mock-function-api#mockfnmockreturnvalueoncevalue"&gt;Mock once&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>testing</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Prototypes &amp; inheritance in JavaScript</title>
      <dc:creator>vazsonyidl</dc:creator>
      <pubDate>Mon, 25 Jan 2021 18:25:59 +0000</pubDate>
      <link>https://dev.to/vazsonyidl/prototypes-inheritance-in-javascript-478</link>
      <guid>https://dev.to/vazsonyidl/prototypes-inheritance-in-javascript-478</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;Have you ever wondered how inheritance works in a language like Javascript? This language is not in the first ones that come to your mind first when talking about OOP paradigms. Although inheritance literally works in JavaScript, and in the following sections we will discuss it in details.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Prototype&lt;/strong&gt;, the stalker
&lt;/h3&gt;

&lt;p&gt;If you are not a real newbie to JavaScript, then maybe the words &lt;em&gt;prototype&lt;/em&gt; and property not sounds strange. Almost all objects in JavaScript have a prototype object. Although &lt;code&gt;Object.prototype&lt;/code&gt; is one of that rare objects that does not have a &lt;em&gt;prototype&lt;/em&gt; object. When you create a simple &lt;/p&gt;

&lt;p&gt;&lt;code&gt;let a = 'bcd';&lt;/code&gt; code, the &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Object.getPrototypeOf(a); // String.prototype&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;will return you some interesting stuff. Your variable has been wrapped around with an object, here for example with the &lt;em&gt;String&lt;/em&gt;. That is the reason why you can use that huge amount of different methods on your variables.&lt;/p&gt;

&lt;p&gt;If you create a simple &lt;code&gt;let q = {};&lt;/code&gt; then you do not have a &lt;em&gt;prototype&lt;/em&gt; property at all, sou can not query with the &lt;code&gt;q.prototype&lt;/code&gt; way. But do not afraid, it is the normal behavior. We will discuss how to inspect the &lt;em&gt;prototype&lt;/em&gt; in the following sections.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One important note: almost all object have a &lt;em&gt;prototype&lt;/em&gt; but do not have a property called &lt;em&gt;prototype&lt;/em&gt;. Although inheritance in JavaScript works even this way.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Prototype chain
&lt;/h3&gt;

&lt;p&gt;Accessing properties of object are just like bubbles underwater. It always goes up and up until reaches its goal. &lt;/p&gt;

&lt;p&gt;For example, take that you want to query the &lt;em&gt;myProperty&lt;/em&gt; property on an &lt;em&gt;myObject&lt;/em&gt; object. If that &lt;em&gt;own&lt;/em&gt; property does not exist on that object, the &lt;em&gt;prototype&lt;/em&gt; object of &lt;em&gt;myObject&lt;/em&gt; is looked up. If the property exists there (on that object), then that is returned. If not, then another level is looked again, until &lt;em&gt;myProperty&lt;/em&gt; is found or the &lt;em&gt;prototype&lt;/em&gt; property turns out to be null. In this case &lt;em&gt;undefined&lt;/em&gt; is returned. As this little description summarized it, the &lt;em&gt;prototype&lt;/em&gt; attributes create a chain, called &lt;strong&gt;prototype chain.&lt;/strong&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;p&gt;&lt;iframe height="400" src="https://jsitor.com/embed/0faO6nB1O"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As you can see here, first we created our base object. Then two descendants are made in a chain-like way. If you query a property it bubbles up in the prototype chain, just like the &lt;em&gt;chair&lt;/em&gt; property does. If one is not exists, then &lt;em&gt;undefined&lt;/em&gt; returned. As you can see, you can overwrite properties, and that will not affect the prototype chain's properties, so setting properties not bubbles up, only getting. &lt;/p&gt;

&lt;p&gt;This is a key feature, because it allows us to create and overwrite inherited properties selectively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exceptions
&lt;/h3&gt;

&lt;p&gt;There is one exception that breaks the basic way of creating property of an object. If the inherited property you wish to overwrite (just like color in the previous example) is an accessor property with a setter method. Then that method will be invoked on your object and &lt;strong&gt;again, leaves the prototype chain unmodified&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="400" src="https://jsitor.com/embed/Yn5GI-gfF?js"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Query the &lt;em&gt;prototype&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;The simplest &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf"&gt;built in&lt;/a&gt; function to check for the prototype is the &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Object.getPrototypeOf()&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;method.&lt;/p&gt;

&lt;p&gt;This is how it works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Object.getPrototypeOf([]);  // Array.prototype
Object.getPrototypeOf('');  // String.prototype
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another useful method is the &lt;code&gt;isPrototypeOf&lt;/code&gt; method. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf"&gt;Docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It returns a boolean value for you as the name suggests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = {z: 1};
let b = Object.create(a);
a.isPrototypeOf(b);  // return true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Set the &lt;em&gt;prototype&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;It is important to note, that every prototype is set when creating a new object with any of the available syntaxes and totally unrecommended to change the prototype of an object.&lt;br&gt;
Although it can be carried out in the following way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = [1, 2];
Object.setPrototypeOf(a, String.prototype);
a.join(); // Throws a TypeError, join not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That why it is not a recommendation to set prototype manually.&lt;/p&gt;

&lt;h4&gt;
  
  
  Footnote __proto__
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto"&gt;__proto__&lt;/a&gt; is an early browser exposed implementation of the prototype attribute. Because a huge amount of code still depends on it, it is mandated for that reason, but &lt;strong&gt;deprecated&lt;/strong&gt; since a long time. You should really avoid this. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;I hope you enjoyed my article, leave your comments below!&lt;br&gt;
The next post will be about Classes &amp;amp; Prototypes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Resources:&lt;br&gt;
David Flanagan: JavaScript, The Definitive Guide&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Scopes in JavaScript</title>
      <dc:creator>vazsonyidl</dc:creator>
      <pubDate>Mon, 04 Jan 2021 19:21:22 +0000</pubDate>
      <link>https://dev.to/vazsonyidl/scopes-in-javascript-5ca2</link>
      <guid>https://dev.to/vazsonyidl/scopes-in-javascript-5ca2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Scope&lt;/strong&gt;: maybe you read a hundred of times the word when reading an article about JS or a post on Stackoverflow. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But what does &lt;em&gt;scope&lt;/em&gt; actually means in JavaScript and how scopes are managed?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's dive into this in the following sections. :) &lt;/p&gt;

&lt;h3&gt;
  
  
  What is scope?
&lt;/h3&gt;

&lt;p&gt;Our program has states. Without this, to be honest, our code would just perform operations, do some API calls or calculate things, etc. But it you want to store these results somewhere, you need variables. &lt;/p&gt;

&lt;p&gt;These variables later can be retrieved, updated and it enables more powerful ways to build applications. But how can you access and for what variables you have access to when a piece of code is running?&lt;/p&gt;

&lt;p&gt;These questions need to be answered with a well defined set of rules that manages the access to variables.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;em&gt;scope&lt;/em&gt; is a set of rules that manage the accessibility of variables.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Type of scopes
&lt;/h2&gt;

&lt;p&gt;There are two &lt;em&gt;main&lt;/em&gt; type of scopes that can be divided into smaller pieces.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Global&lt;/em&gt; scope&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Local&lt;/em&gt; scope&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Global scope
&lt;/h3&gt;

&lt;p&gt;Global scope is the most outer scope of the existing scopes. Variables created in the global scope can be accessed within any scope.&lt;/p&gt;

&lt;p&gt;Any variables declared on the global scope is called a &lt;em&gt;global&lt;/em&gt; variable. Although, it is not a good idea to pollute the global scope with variables. &lt;/p&gt;

&lt;p&gt;Because any block of code has access to the global variable set, collisions can happen easily which may overwrite or update the values stored in that global variable accidentally.&lt;/p&gt;

&lt;p&gt;To avoid this problem, let me introduce the &lt;em&gt;local&lt;/em&gt; scope which is helpful in these cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Local scope
&lt;/h3&gt;

&lt;p&gt;Variables and constants declared within any kind of &lt;em&gt;block&lt;/em&gt; is somewhat local scoped and does not pollute the global environment. &lt;br&gt;
These can be created with &lt;em&gt;{}&lt;/em&gt; braces but do not let the chance of understanding slip away and be more precise than this. &lt;br&gt;
In the following sections we will walk through the types of &lt;em&gt;local&lt;/em&gt; scopes.&lt;/p&gt;
&lt;h4&gt;
  
  
  Function scope - var &amp;amp; let &amp;amp; const
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Create scope for var, let and const&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you think about how can you create a scope for your variables, maybe it comes to your mind that a function can come in handy. &lt;br&gt;
Functions in JavaScript create a new scope called &lt;em&gt;function scope&lt;/em&gt;. &lt;br&gt;
The another useful tool of function scopes is closures but it is a good theme for another post. :) &lt;/p&gt;

&lt;p&gt;An example for the better understanding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function _fscope() {
  let a = 1;
  console.log(a); // outputs: 1
}
console.log(a); // ReferenceError: a is not defined

globalThis.a // evaluates to undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, a simple function declaration creates a scope for the variables declared within it. You can not access to that variable outside of that &lt;em&gt;scope&lt;/em&gt;.&lt;br&gt;
An &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/IIFE"&gt;IIFE&lt;/a&gt; creates a scope as well as basic function declarations.&lt;/p&gt;
&lt;h4&gt;
  
  
  Block scope - let &amp;amp; const
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Create scope for let and const&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A block scope now is really can be described as a scope within &lt;em&gt;{}&lt;/em&gt; curly braces or another way: a JavaScript block of code defines a scope for variables.&lt;/p&gt;

&lt;p&gt;The following example can give a quick view of its behavior.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  let q = 123;
  console.log(q); // 123
}
console.log(q); // ReferenceError: q is not defined

globalThis.q // evaluates to undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is really the simplest example. The following &lt;em&gt;blocks&lt;/em&gt; create &lt;em&gt;block scope&lt;/em&gt; for variables.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loops&lt;/li&gt;
&lt;li&gt;Conditional statements 'body'&lt;/li&gt;
&lt;li&gt;A simple {} block&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Nesting scopes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Scopes can be nested, which means: any of the 'inner' scope has access to the containing scopes up to the global object. Or if you start from the other end of the statement, none of the 'outer' scopes has access to the 'inner' scopes. It shouts for an example!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const global = '2021';
(function() {
    let outer = 123;
  if (outer) {
    const inner = 'inner';
    console.log(global, inner, outer); // '2021' 'inner' 123
  };
    console.log(global, inner, outer); // ReferenceError: inner is not defined
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see in the above example, a nested scope has access to all of the outer scopes, but a scope does not have access to its inner ones.&lt;/p&gt;

&lt;h4&gt;
  
  
  Module scope
&lt;/h4&gt;

&lt;p&gt;ES2015, or maybe you known it as ES6 introduces &lt;em&gt;modules&lt;/em&gt;. Modules are just a simple &lt;em&gt;.js&lt;/em&gt; or &lt;em&gt;.mjs&lt;/em&gt; file. It does not matter is a module is CommonJS module or an ES6 module, it creates a scope for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;li&gt;Classes&lt;/li&gt;
&lt;li&gt;Variables&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So unless you not export one of the above mentioned member of the module, it is not available in another module, even if you import that module. It is a good separation mechanism.&lt;/p&gt;

&lt;h3&gt;
  
  
  The +1: &lt;strong&gt;var&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As you see above, only the function scope can encapsulate a variable created with &lt;em&gt;var&lt;/em&gt; (and this is +1 reason why using var is not recommended nowadays).&lt;/p&gt;

&lt;p&gt;Sometimes I find debates whether &lt;em&gt;var&lt;/em&gt; creates variables on the global scope. Let revisit this part os JavaScript.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Creating variables with &lt;em&gt;var&lt;/em&gt; &lt;strong&gt;not&lt;/strong&gt; creates variable on the global scope. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Variables created with &lt;em&gt;var&lt;/em&gt; is &lt;strong&gt;function scoped&lt;/strong&gt;, so the following will output &lt;em&gt;undefined&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(function (){
    var q = 3;
})();
console.log(globalThis.q);
*undefined*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the following will output a not so nice thing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(true) {
  let a = 3;
  var b = 2;
}
console.log(globalThis.a); // undefined
console.log(globalThis.b); // 2 - oops :( 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My recommendation: try to use let everywhere except there is a very-very strong reason for using var.&lt;/p&gt;

&lt;p&gt;The related article can be found on the official &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var"&gt;Docs&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding scopes is an important part in the long learning path of JavaScript, but definitely can helps reduce debug time and gives a better understand of how the JS codes working.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed my article, please add your comments below! :) &lt;/p&gt;

&lt;p&gt;@Cover image: &lt;a href="https://unsplash.com/@simonmaage"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>NodeJS modularizaton &amp; strict mode</title>
      <dc:creator>vazsonyidl</dc:creator>
      <pubDate>Fri, 25 Dec 2020 17:50:20 +0000</pubDate>
      <link>https://dev.to/vazsonyidl/nodejs-modularizaton-strict-mode-1fhb</link>
      <guid>https://dev.to/vazsonyidl/nodejs-modularizaton-strict-mode-1fhb</guid>
      <description>&lt;p&gt;Maybe you know a lot about the &lt;code&gt;strict mode&lt;/code&gt; in Javascript/ ECMAscript. It is quite a useful stuff and it is strongly recommended to use always to throw error during the development and avoid confusing bugs.&lt;/p&gt;

&lt;p&gt;You can read more about strict mode here on the official &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode"&gt;Docs&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Strict mode in NodeJs
&lt;/h2&gt;

&lt;p&gt;Maybe when you start developing a NodeJS project or a simple application, it comes to your mind: do I have to put the &lt;code&gt;"use strict";&lt;/code&gt; boilerplate onto the first line of every &lt;em&gt;.js&lt;/em&gt; file in my project?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Depends on your configuration, you have to and also you do not have to.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Keep reading, I will explain this in details.&lt;/p&gt;

&lt;h3&gt;
  
  
  Default mode in Node JS
&lt;/h3&gt;

&lt;p&gt;The default NodeJS set up, when you initialize your project first for example with the &lt;code&gt;npm init&lt;/code&gt; command &lt;strong&gt;does not use strict mode&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NodeJS uses &lt;a href="https://nodejs.org/api/modules.html#modules_modules_commonjs_modules"&gt;CommonJS&lt;/a&gt; modularization by default.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So if you hang up with this configuration, you end up &lt;strong&gt;not using strict mode&lt;/strong&gt; in any of your files.&lt;/p&gt;

&lt;h4&gt;
  
  
  Non strict mode example
&lt;/h4&gt;

&lt;p&gt;For clarity, let me show you some code snippets.&lt;/p&gt;

&lt;p&gt;I have an &lt;em&gt;app.js&lt;/em&gt; file containing the following setup (I import the modules this way to emphasize the different modes)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nonStrictMode = require('./modules/non-strict');
nonStrictMode.preventExtension();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the following &lt;em&gt;non-strict.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
    preventExtension: () =&amp;gt; {
        console.log('Non strict mode.');
        const fixed = {};
        Object.preventExtensions(fixed);
        fixed.newProperty = 'newProp';
        console.log(fixed.newProperty);
        console.log('Non strict mode finished.');
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is a full valid JavaScript syntax in non strict mode. Although it will not work as the way you expect, you will not get a &lt;em&gt;compile&lt;/em&gt; time error, and you will be confused maybe.&lt;/p&gt;

&lt;p&gt;The console output will be the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Non strict mode.
*undefined*
Non strict mode finished.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hmm, maybe that is not what you want to achieve. So it would be better if an error will be thrown to inform you about this 'problem'.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Strict mode for the rescue.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Strict mode example
&lt;/h4&gt;

&lt;p&gt;So I have the following configuration in my &lt;em&gt;app.js&lt;/em&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const strictMode = require('./modules/strict');
strictMode.preventExtension();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the following code in my &lt;em&gt;strict.js&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'use strict';

module.exports = {
    preventExtension: () =&amp;gt; {
        console.log('Strict mode on.');
        const fixed = {};
        Object.preventExtensions(fixed);
        fixed.newProperty = 'newProp';
        console.log('Strict mode finished.');
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will be a nice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Strict mode on.
TypeError: Cannot add property newProperty, object is not extensible.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Wait, wait, is it not possible to add properties to a freezed object?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;No it is not, and it is far more better to get a &lt;em&gt;compile&lt;/em&gt; time error than avoiding this error at all.&lt;/p&gt;

&lt;p&gt;As you can see, I used the &lt;strong&gt;'use strict';&lt;/strong&gt; at the top of this &lt;em&gt;.js&lt;/em&gt; file. So maybe you had better to use this configuration as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Modules on ECMAScript
&lt;/h2&gt;

&lt;p&gt;The modularization was announced in the 6th edition of &lt;a href="https://en.wikipedia.org/wiki/ECMAScript#6th_Edition_%E2%80%93_ECMAScript_2015"&gt;ECMAScript&lt;/a&gt; (released on 2015), known as ES6.&lt;/p&gt;

&lt;p&gt;It is such a big help for complex applications.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ECMAScript modules uses strict mode by default.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It comes in handy if you want to omit all the boilerplate from the first line of files and enforce some coding restrictions with the &lt;em&gt;strict&lt;/em&gt; mode.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enforcing modularization for NodeJS
&lt;/h3&gt;

&lt;p&gt;If you are initialize a project with the &lt;em&gt;npm init&lt;/em&gt;, a &lt;em&gt;package.json&lt;/em&gt; file will be added to your root directory.&lt;/p&gt;

&lt;p&gt;In this &lt;em&gt;package.json&lt;/em&gt; will have to look like something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "js",
  "version": "1.0.0",
  "description": "Strict mode compare",
  "main": "app.js",
  "type": "module",
  "author": "You",
  "license": "MIT"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the &lt;code&gt;'type': 'module'&lt;/code&gt; property. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;With this property, you can enforce your NodeJS app to use the modularization described in the ECMAScript 6 rules.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Modularized example
&lt;/h4&gt;

&lt;p&gt;My &lt;em&gt;app.js&lt;/em&gt; and the &lt;em&gt;modularized.js&lt;/em&gt; look like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {modularizedPreventExtension} from 'modularized.js';
modularizedPreventExtension();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function modularizedPreventExtension() {
    console.log('Modularized mode on.');
    const fixed = {};
    Object.preventExtensions(fixed);
    fixed.newProperty = 'newProp';
    console.log('Modularized mode finished.');
}

export {modularizedPreventExtension};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;End as you expect, the output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Modularized mode on.
TypeError: Cannot add property newProperty, object is not extensible
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Additional information
&lt;/h2&gt;

&lt;p&gt;So you are wondering maybe how the app knows whether to use or not to use this type of modularization. It is totally based on your &lt;strong&gt;closest&lt;/strong&gt; &lt;em&gt;package.json&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I truly recommend not to mix this types, just simply use the modularization provided y ES6.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed my article, please add your reviews in the comments below! :) &lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://nodejs.org/api/modules.html#modules_modules_commonjs_modules"&gt;CommonJS modules&lt;/a&gt;&lt;br&gt;
&lt;a href="https://nodejs.org/api/esm.html#esm_modules_ecmascript_modules"&gt;ECMAScript modules&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Intro to Array.reduce()</title>
      <dc:creator>vazsonyidl</dc:creator>
      <pubDate>Sun, 29 Nov 2020 10:57:09 +0000</pubDate>
      <link>https://dev.to/vazsonyidl/intro-to-array-reduce-494k</link>
      <guid>https://dev.to/vazsonyidl/intro-to-array-reduce-494k</guid>
      <description>&lt;h2&gt;
  
  
  Array functions
&lt;/h2&gt;

&lt;p&gt;There are some basic array function which about every developer has some knowledge, or at least some idea what that functions do. These functions are: &lt;em&gt;sort&lt;/em&gt;, &lt;em&gt;map&lt;/em&gt;, &lt;em&gt;findIndex&lt;/em&gt;, &lt;em&gt;includes&lt;/em&gt;. But there are some powerful Array functions that good to know about. Maybe someone start using this later in their project or with this article it would be easier to understand he Array.reduce() function. &lt;/p&gt;

&lt;h3&gt;
  
  
  Problems
&lt;/h3&gt;

&lt;p&gt;What if you have an array of numbers, and want to calculate the sum of every element? Maybe you loop through over the array, create a sum property, set to zero and add the values together, and the end of the function return with the summed value. Something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numArray = [1, 4, 5 ,6 ,8];
let sum = 0;

for (const num of numArray) {
    sum += num;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have multiple arrays to add together, you have to do calculate the sums multiple times, or write a function that accept an array parameter and return the expected sum of the array than add that values together. &lt;/p&gt;

&lt;p&gt;What if you want to calculate the sum of the factorial of an array? Okay, perhaps you never want to do this, but imagine ... &lt;/p&gt;

&lt;h3&gt;
  
  
  Reduce function
&lt;/h3&gt;

&lt;p&gt;In above mentioned use cases the reduce function can come in handy. According to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce"&gt;MDN Docs&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The &lt;code&gt;reduce()&lt;/code&gt; method executes a **reducer&lt;/em&gt;* function (that you provide) on each element of the array, resulting in &lt;strong&gt;single output value.&lt;/strong&gt;*&lt;/p&gt;

&lt;p&gt;The functions accepts a callback function with 2 required and 2 optional parameter. The first 2 parameters are:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;accumulator &amp;amp; current value &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the naming make someone confused, the simplest way to think about the accumulator: a temporary storage for the computed values in the previous loops. &lt;br&gt;
The current value has a more clear name: it holds the value of the current iteration (something like array[index]).&lt;/p&gt;

&lt;p&gt;The last 2 parameter is &lt;em&gt;optional&lt;/em&gt;, but just for mention: the third one is the currently processed index (&lt;em&gt;starts from zero when initial value given, start from 1 when initial value is leaved empty&lt;/em&gt;) of the array, and the fourth one is the array on currently calling the reduce function. &lt;/p&gt;
&lt;h3&gt;
  
  
  Initial value
&lt;/h3&gt;

&lt;p&gt;You can provide initial value for the reduce function. If no initial value is provided, the first element of the array will be the initial value (and the index property - third argument of the function - will start from &lt;strong&gt;1&lt;/strong&gt;!)&lt;br&gt;
But enough talking, lets see some examples that can solve the missing pieces now.&lt;/p&gt;
&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;

&lt;p&gt;Lets revisit the sum of an array with the reduce function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const reduceSum = numArray.reduce((alreadySummed, current) =&amp;gt; alreadySummed + current, 0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we try to examine the values in each iteration for the above mentioned array, the following stands:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;iteration number&lt;/th&gt;
&lt;th&gt;temp container&lt;/th&gt;
&lt;th&gt;current value&lt;/th&gt;
&lt;th&gt;return value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1st&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2nd&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3rd&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4th&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5th&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If no initial value provided, the reduce function simply skip (but set every property according to) the first row mentioned in this table, and everything goes on from the second row.&lt;/p&gt;

&lt;h3&gt;
  
  
  Future discussion
&lt;/h3&gt;

&lt;p&gt;Of course there are several more sophisticated, complicated usage of the reduce function. You can count values in array of objects based on a key, transform values according to an ENUM. You can return an array or an object from the function not only a number or a string. This is just an introduction to the function.&lt;br&gt;
Hope you find it interesting, this is my first post here. Please add your remarks in the discussion! &lt;/p&gt;

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