<?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: Julián Herrera</title>
    <description>The latest articles on DEV Community by Julián Herrera (@vidjuheffex).</description>
    <link>https://dev.to/vidjuheffex</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%2F31439%2F0f095cf7-b7d7-4a3b-a62d-3e3580e33026.jpeg</url>
      <title>DEV Community: Julián Herrera</title>
      <link>https://dev.to/vidjuheffex</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vidjuheffex"/>
    <language>en</language>
    <item>
      <title>Using A NodeList as an Array: A Practical Use for Object Composition</title>
      <dc:creator>Julián Herrera</dc:creator>
      <pubDate>Mon, 28 Aug 2017 19:43:25 +0000</pubDate>
      <link>https://dev.to/vidjuheffex/using-a-nodelist-as-an-array-a-practical-use-for-object-composition</link>
      <guid>https://dev.to/vidjuheffex/using-a-nodelist-as-an-array-a-practical-use-for-object-composition</guid>
      <description>&lt;h1&gt;
  
  
  Using A NodeList as an Array
&lt;/h1&gt;

&lt;h2&gt;
  
  
  The Scenario:
&lt;/h2&gt;

&lt;p&gt;I had queried a bunch of tags, using querySelectorAll(), and received a NodeList in return.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem:
&lt;/h2&gt;

&lt;p&gt;NodeLists are like arrays (i.e. they have a length property, they are accessed by an index in brackets: NodeList[0]) however, try using .map, or .filter, or .forEach on one. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Approach:
&lt;/h2&gt;

&lt;p&gt;The options out there were varied. From looping through and filling an Array to some more clever es6 options like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;elements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt; &lt;span class="nx"&gt;nodelist&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;elements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nodelist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, these have a problem... they worked too well. You now had an array INSTEAD of a NodeList. Sure, it had all the data from the NodeList but it no longer identified itself as a NodeList.&lt;br&gt;
&lt;strong&gt;What's the problem with that?&lt;/strong&gt;&lt;br&gt;
Try:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;nodeElementInTheArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;compareDocumentPosition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;anotherNodeElementInTheArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will error out because the argument is not a true NodeListItem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lets reframe our needs
&lt;/h3&gt;

&lt;p&gt;We don't need our NodeList to be an Array, we just need those properties from Arrays. This is the perfect place for object composition.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;NODELIST&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our NodeList remains a NodeList, and it acquires those Array traits we need while not modifying its prototype. I didn't see this solution anywhere, likely because it's never what is asked for. So instead of asking, how do I make A become B, ask how can I get A to behave like B, and the answer will likely be 'Object Composition'  &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
      <category>dom</category>
    </item>
  </channel>
</rss>
