<?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: Alex Bratsos</title>
    <description>The latest articles on DEV Community by Alex Bratsos (@alexbratsos).</description>
    <link>https://dev.to/alexbratsos</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%2F187502%2F745034cd-9a89-462e-878c-eeb4e260bcb5.jpg</url>
      <title>DEV Community: Alex Bratsos</title>
      <link>https://dev.to/alexbratsos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexbratsos"/>
    <language>en</language>
    <item>
      <title>Null Object spread horror story</title>
      <dc:creator>Alex Bratsos</dc:creator>
      <pubDate>Sun, 07 Nov 2021 10:25:15 +0000</pubDate>
      <link>https://dev.to/alexbratsos/null-object-spread-horror-story-p07</link>
      <guid>https://dev.to/alexbratsos/null-object-spread-horror-story-p07</guid>
      <description>&lt;p&gt;While most of the modern frontend engineers use object spread syntax a lot in their code, we all overcome some simple details and underlying mechanisms of how it actually works.&lt;/p&gt;

&lt;p&gt;With a first look, this code looks like something that would break right?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
object? = {...123} // =&amp;gt; {} Wait what? Is this valid??
object? = {...undefined} // =&amp;gt; {} Um, wat?
object? = {...null} =&amp;gt; // {} Please stop
object? = {...false} =&amp;gt; // {} Ok I'm done, bye javascript
object? = {...'Smallpdf'} // =&amp;gt; {0: "S", 1: "m", 2: "a", 3: "l", 4: "l", 5: "p", 6: "d", 7: "f"}
*/&lt;/span&gt;

&lt;span class="c1"&gt;// Did we break javascript??!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Probably we would expect a &lt;code&gt;TypeError&lt;/code&gt; here. But we must not forget that &lt;code&gt;...&lt;/code&gt; is a syntax code, not an operator. So the result of it depends on the surrounding context. It behaves differently if it's in an array (&lt;code&gt;[...myArr]&lt;/code&gt;), in an object (&lt;code&gt;{...myObj}&lt;/code&gt;), or in a function argument list (&lt;code&gt;myFunc(arg1, ..restArgs&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;So let's see what happens &lt;em&gt;exactly&lt;/em&gt; when it is used inside an object.&lt;/p&gt;

&lt;p&gt;According to TC39, object spread initializer is &lt;a href="https://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md" rel="noopener noreferrer"&gt;a syntactic sugar&lt;/a&gt; on top of &lt;code&gt;Object.assign&lt;/code&gt;. So the next logical step is to see how the &lt;code&gt;Object.assign&lt;/code&gt; &lt;em&gt;should&lt;/em&gt; work, as instructed by the &lt;a href="https://262.ecma-international.org/6.0/#sec-object.assign" rel="noopener noreferrer"&gt;ECMAscript spec.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2omyt1mg2d8p0ev43r98.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%2F2omyt1mg2d8p0ev43r98.png" alt="Screenshot from ECMAScript spec explaining how Object.assign should work"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our case, when using the &lt;code&gt;{...something}&lt;/code&gt; syntax, the object expression (&lt;code&gt;{}&lt;/code&gt;) is the &lt;code&gt;target&lt;/code&gt; so it's a newly created object and &lt;code&gt;sources&lt;/code&gt; is whatever we pass after the &lt;code&gt;...&lt;/code&gt; syntax, so in our case it's &lt;code&gt;something&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now if &lt;code&gt;something&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; we can see an explicit instruction of how &lt;code&gt;Object.assign&lt;/code&gt; should handle this, treat it like an empty &lt;code&gt;List&lt;/code&gt; so our end result will just ignore it. This explains why &lt;code&gt;{...undefined}&lt;/code&gt; and &lt;code&gt;{...null}&lt;/code&gt; returns an empty object and doesn't crash in any way.&lt;/p&gt;

&lt;p&gt;But what happens with &lt;code&gt;false&lt;/code&gt; &lt;code&gt;123&lt;/code&gt; and &lt;code&gt;'Smallpdf'&lt;/code&gt;? Let's go back to the ECMAscript spec&lt;/p&gt;

&lt;p&gt;After explicitly handling &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;null&lt;/code&gt; cases it concludes with the next steps:&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%2F2zt8l268dgbumrgak8x4.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%2F2zt8l268dgbumrgak8x4.png" alt="Screenshot from ECMAScript spec explaining what should happen when value is null or undefined"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we see that for other types of arguments, (except &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;) the spec uses the &lt;code&gt;ToObject&lt;/code&gt; abstract operation, to convert the value to an object and if the return value is not &lt;code&gt;undefined&lt;/code&gt; it will try to use the enumerable properties of the result. Keep in mind that &lt;code&gt;ToObject&lt;/code&gt; conversions are &lt;a href="https://262.ecma-international.org/6.0/#sec-toobject" rel="noopener noreferrer"&gt;described in the table below&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ts9dpue29aibr8hxyd3.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%2F3ts9dpue29aibr8hxyd3.png" alt="Screenshot from ECMAScript spec with a detailed table explaining what ToObject conversion should do depending on the argument type"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we try to code this we will get the following results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ToObject conversion&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;NumberObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BooleanObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;StringObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Smallpdf&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Get properties for each items, and return enumerable properties to our object&lt;/span&gt;

&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getOwnPropertyDescriptors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;NumberObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; {}&lt;/span&gt;
&lt;span class="c1"&gt;// So object? = {...123} =&amp;gt; {} makes sense&lt;/span&gt;

&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getOwnPropertyDescriptors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;BooleanObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; {}&lt;/span&gt;
&lt;span class="c1"&gt;// object? = {...false} =&amp;gt; {} yup&lt;/span&gt;

&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getOwnPropertyDescriptors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;StringObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="cm"&gt;/* =&amp;gt;
0: {value: "S", writable: false, enumerable: true, configurable: false}
1: {value: "m", writable: false, enumerable: true, configurable: false}
2: {value: "a", writable: false, enumerable: true, configurable: false}
3: {value: "l", writable: false, enumerable: true, configurable: false}
4: {value: "l", writable: false, enumerable: true, configurable: false}
5: {value: "p", writable: false, enumerable: true, configurable: false}
6: {value: "d", writable: false, enumerable: true, configurable: false}
7: {value: "f", writable: false, enumerable: true, configurable: false}
length: {value: 8, writable: false, enumerable: false, configurable: false}

*/&lt;/span&gt;

&lt;span class="c1"&gt;// So according to the spec, we take only the `enumerable: true` properties&lt;/span&gt;
&lt;span class="c1"&gt;// from this object. Finally we use their `keys` (0, 1, 2, 3, 4, 5, 6, 7)&lt;/span&gt;
&lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;their&lt;/span&gt; &lt;span class="s2"&gt;`value`&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;S&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;m&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;l&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;l&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;d&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;f&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="nx"&gt;them&lt;/span&gt;
&lt;span class="nx"&gt;into&lt;/span&gt; &lt;span class="nx"&gt;our&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="c1"&gt;// object? = {...'Smallpdf'} // =&amp;gt; {0: "S", 1: "m", 2: "a", 3: "l", 4: "l", 5: "p", 6: "d", 7: "f"}&lt;/span&gt;
&lt;span class="c1"&gt;// it all makes sense now&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Javascript surely is weird, but if we follow the spec, it all makes sense! 🌈 🎉&lt;/p&gt;

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