<?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: Yoel Macia</title>
    <description>The latest articles on DEV Community by Yoel Macia (@galiciandeveloper).</description>
    <link>https://dev.to/galiciandeveloper</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%2F199982%2F2f98a2e9-a8a2-4ecd-ab71-e3462cb55def.jpeg</url>
      <title>DEV Community: Yoel Macia</title>
      <link>https://dev.to/galiciandeveloper</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/galiciandeveloper"/>
    <language>en</language>
    <item>
      <title>WeakMaps in Javascript</title>
      <dc:creator>Yoel Macia</dc:creator>
      <pubDate>Thu, 12 Dec 2019 11:28:38 +0000</pubDate>
      <link>https://dev.to/galiciandeveloper/weakmaps-in-javascript-1hfp</link>
      <guid>https://dev.to/galiciandeveloper/weakmaps-in-javascript-1hfp</guid>
      <description>&lt;p&gt;The &lt;strong&gt;WeakMap&lt;/strong&gt; object is a collection of key/value pairs in which the keys are weakly referenced. The keys must be objects and the values can be arbitrary values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bbtXt2ng--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2AZK-Y_vEzMp1bEhFKTzqllQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bbtXt2ng--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2AZK-Y_vEzMp1bEhFKTzqllQ.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s talk about we have a class person and a property name.&lt;/p&gt;

&lt;p&gt;Here is the example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person {  
   constructor(name) {  
       this.name = name;  
   }  
}

let Person1 = new Person("John");

console.log(Person1); // Person { name: 'John' }  
console.log(Person1.name); // John
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We need to have a private variable in Javascript so that that name does not have access from the outside.&lt;/p&gt;

&lt;p&gt;How we can do it ?&lt;/p&gt;

&lt;p&gt;One solution would be to put underscores, for example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person {  
   constructor(name) {  
       this.\_name = name;  
   }  
}

let Person1 = new Person("John");

console.log(Person1); // Person { name: 'John' }  
console.log(Person1.name); // Undefined
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This brings with it the eternal struggle to warn everyone that underscoring cannot be changed or touched.&lt;/p&gt;

&lt;p&gt;Another solution would be to use the data type Symbol(),&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let s\_name = Symbol();

class Person {  
   constructor(name) {  
       this\[s\_name\] = name;  
   }  
}

let Person1 = new Person("John");

console.log(Person1); // Person { name: 'John' }  
console.log(Person1.name); // Undefined
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The problem is than Symbols are exposed with methods like &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols"&gt;Object.getOwnPropertySymbols()&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;let symbols = Object.getOwnPropertySymbols(Person1);&lt;/p&gt;

&lt;p&gt;console.log(Person1[symbols[0]]); // John&lt;/p&gt;

&lt;p&gt;But the most simple option is,&lt;/p&gt;

&lt;h3&gt;
  
  
  new WeakMap( [iterable] );
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;This object have one argument (iterable)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;iterable&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;-&amp;gt; Optional.&lt;/em&gt; Iterable is an Array or other iterable object whose elements are key-value pairs (2-element Arrays). Each key-value pair will be added to the new WeakMap. null is treated as undefined.&lt;/p&gt;

&lt;p&gt;So let´s create our WeakMap,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person {  
   constructor(name) {  
     let weakmap = new WeakMap();  
     weakmap.set(this, { name: name });  
   }  
}

let Person1 = new Person("John");

console.log(Person1); // Person {}  
console.log(Person1.name); // Undefined
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  The main differences to the Map object are
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  WeakMap are &lt;strong&gt;collections of objects only.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  If an element object has no other reference left, it will be auto released to the &lt;strong&gt;garbage collector&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;  For the previous reason you can´t &lt;strong&gt;iterate&lt;/strong&gt; over objects inside of WeakMap.&lt;/li&gt;
&lt;li&gt;  For the previous reason you can´t know the &lt;strong&gt;size&lt;/strong&gt; of the objects inside of WeakMap.&lt;/li&gt;
&lt;li&gt;  You can only use &lt;strong&gt;.set()&lt;/strong&gt;, &lt;strong&gt;get()&lt;/strong&gt;, &lt;strong&gt;.has()&lt;/strong&gt; and &lt;strong&gt;.delete()&lt;/strong&gt; methods with WeakMap.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;WeakMaps can be particularly useful constructs when mapping keys to information about the key that is valuable only if the key has not been garbage collected.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I often share more code tips on my&lt;/em&gt; &lt;a href="https://www.instagram.com/galiciandeveloper/"&gt;&lt;strong&gt;&lt;em&gt;Instagram&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;  &lt;em&gt;you can say hello to me on my&lt;/em&gt; &lt;a href="https://twitter.com/galiciandevelop"&gt;&lt;strong&gt;&lt;em&gt;Twitter&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;  &lt;em&gt;or see how i code in&lt;/em&gt; my &lt;a href="https://github.com/yoelmacia"&gt;&lt;strong&gt;&lt;em&gt;Github&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thank you very much and keep coding!!!&lt;/p&gt;

&lt;p&gt;By &lt;a href="https://medium.com/@galiciandeveloper"&gt;Yoel Macia&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>weakmaps</category>
      <category>privatevars</category>
    </item>
    <item>
      <title>WeakSets in Javascript</title>
      <dc:creator>Yoel Macia</dc:creator>
      <pubDate>Wed, 11 Dec 2019 08:18:31 +0000</pubDate>
      <link>https://dev.to/galiciandeveloper/weaksets-in-javascript-1ef3</link>
      <guid>https://dev.to/galiciandeveloper/weaksets-in-javascript-1ef3</guid>
      <description>&lt;h1&gt;
  
  
  WeakSets in Javascript
&lt;/h1&gt;

&lt;p&gt;The  &lt;strong&gt;weakSet&lt;/strong&gt;  object lets you store weakly held  &lt;em&gt;objects&lt;/em&gt;  in a collection.&lt;/p&gt;

&lt;p&gt;Let’s talk about the fact that we have a array of objects with emails.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let emails = [{  
 from: "Galician Developer",  
 to: "admin@example.com",  
 text: "Posting daily javascript articles"  
},  
{  
 from: "Addy Osmani",  
 to: "admin@example2.com",  
 text: "Google Chrome doesnt work!"  
},  
{  
 from: "Umar Hansa",  
 to: "admin@example3.com",  
 text: "Shazam!!! Let work, we have no time"  
}];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We want to add them to a read folder when we read them. If we delete an email we also want to delete it from the read folder.&lt;/p&gt;

&lt;p&gt;How we can do it?&lt;/p&gt;

&lt;p&gt;One solution would be, for example, to save the first element in other array and when we delete the first email also delete the first email read from this array.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let markAsReadFolder [];
markAsReadFolder.push(emails[0]); // Read the first email object

console.log(markAsReadFolder);   
// Output  
{  
from: "Galician Developer",  
to: "admin@example.com",  
text: "Posting daily javascript articles"  
}
emails.shift(); // Delete first object from emails  
markAsReadFolder.shift(); // Delete first object from read folder

console.log(markAsReadFolder);   
// Output  
[]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Other solution would be find the index of the first element than we are going to delete and then use splice().&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let markAsReadFolder = [];
markAsReadFolder.push(emails[0]); // Read the first email object

console.log(markAsReadFolder);  
// Output  
{  
 from: "Galician Developer",  
 to: "admin@example.com",  
 text: "Posting daily javascript articles"  
}

let indexEmails = emails.indexOf(0);  
emails.splice(indexEmails, 1); // Delete first object from emails
let indexRF = markAsReadFolder.indexOf(0);  
markAsReadFolder.splice(indexRF, 1); // Delete first object from folder

console.log(markAsReadFolder);  
// Output  
[]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But the most simple option is,&lt;/p&gt;

&lt;h1&gt;
  
  
  new WeakSet( [iterable] );
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;This object have one argument (iterable)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;iterable&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;-&amp;gt; Optional.&lt;/em&gt; If an  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of"&gt;iterable object&lt;/a&gt; is passed, all of its elements will be added to the new  &lt;code&gt;WeakSet&lt;/code&gt;. null is treated as undefined.&lt;/p&gt;

&lt;p&gt;So let´s create our WeakSet,&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let markAsReadFolder = new WeakSet();
markAsReadFolder.add(emails[0]); // Read the first email object

console.log("Have we read the first email? " + markAsReadFolder.has(emails[0])); // true

emails.shift(); // Delete first object from emails

console.log("Have we read the first email? " + markAsReadFolder.has(emails[0])); // false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As we can see when you delete the object in question from the array of emails is also deleted from our WeakSet.&lt;/p&gt;

&lt;h2&gt;
  
  
  The main differences to the  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"&gt;Set&lt;/a&gt;  object are:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  WeakSets are  &lt;strong&gt;collections of objects only.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  If an element object has no other reference left, it will be auto released to the  &lt;strong&gt;garbage collector&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;  For the previous reason you can´t  &lt;strong&gt;iterate&lt;/strong&gt;  over objects inside of WeakSet.&lt;/li&gt;
&lt;li&gt;  For the previous reason you can´t know the  &lt;strong&gt;size&lt;/strong&gt; of the objects inside of WeakSet.&lt;/li&gt;
&lt;li&gt;  You can only use  &lt;strong&gt;.add()&lt;/strong&gt;,  &lt;strong&gt;.has()&lt;/strong&gt;  and  &lt;strong&gt;.delete()&lt;/strong&gt;  methods with WeakSet.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Aren’t you sure about the garbage collector?
&lt;/h2&gt;

&lt;p&gt;I’m going to share with you a small project in which you can see the differences in the garbage collector between the Set and the WeakSet.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://codepen.io/GalicianDeveloper/pen/dyPMRYb"&gt;Codepen Set vs WeakSet&lt;/a&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  You can find links to my other articles on Medium  &lt;a href="https://medium.com/@galiciandeveloper"&gt;here&lt;/a&gt;.
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;I often share more code tips on my&lt;/em&gt; &lt;a href="https://www.instagram.com/galiciandeveloper/"&gt;&lt;strong&gt;&lt;em&gt;Instagram&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;  &lt;em&gt;you can say hello to me on my&lt;/em&gt; &lt;a href="https://twitter.com/galiciandevelop"&gt;&lt;strong&gt;&lt;em&gt;Twitter&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;  &lt;em&gt;or see how i code in&lt;/em&gt;  my  &lt;a href="https://github.com/yoelmacia"&gt;&lt;strong&gt;&lt;em&gt;Github&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thank you very much and keep coding!!!&lt;/p&gt;

&lt;p&gt;Yoel&lt;/p&gt;

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