<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged javascript-objects</title>
<link>https://coderlegion.com/tag/javascript-objects</link>
<description>Powered by Question2Answer</description>
<item>
<title>How I talk with the DOM?</title>
<link>https://coderlegion.com/5279/how-i-talk-with-the-dom</link>
<description>&lt;h3 id=&quot;intro-&quot;&gt;Intro.&lt;/h3&gt;
&lt;p&gt;In the past I made use of the &quot;document.querySelector(All) &quot; all over the place and what often resulted in &quot;undefined&quot; errors.&lt;/p&gt;
&lt;p&gt;To be honest, it took me a long time to find a solution for this. &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;At first I began with creating a function for the &quot;document.querySelector(All) (that I to date still use!)&quot;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre style=&quot;background:#62686d; color:#fff&quot;&gt;
export async function elQuery(...args){
  const [elem,el_all=false,el_parent] = args;
  let el;
  if(true === el_all){
    if(el_parent) el = el_parent.querySelectorAll(elem);
    else el = document.querySelectorAll(elem);
  }else{
    if(el_parent) el = el_parent.querySelector(elem);
    else el = document.querySelector(elem);
  }
  return await el;
}
&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Swapping function calls up and down within my index.js. &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using &quot;setTimeout()&quot; and so on!&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There were some improvements with this steps but it was still struggling to get my queries right.&lt;/p&gt;
&lt;h3 id=&quot;a-quantum-leap-forward-&quot;&gt;A Quantum Leap forward.&lt;/h3&gt;
&lt;p&gt;Finally I found a way to get this done by using &lt;strong&gt;Map()&lt;/strong&gt; and that is what I want to share with you. &lt;/p&gt;
&lt;h3 id=&quot;first-this-&quot;&gt;First this.&lt;/h3&gt;
&lt;p&gt;I use javascript with a module approach, working with a folder/file structure and a bunch of custom (reusable) functions, aside of that I use classes too.&lt;/p&gt;
&lt;p&gt;The given example here is a reflection of that!&lt;/p&gt;
&lt;h5 id=&quot;1-structure-&quot;&gt;1 structure:&lt;/h5&gt;
&lt;pre style=&quot;background:#62686d; color:#fff&quot;&gt;
assets/
  scripts/
    factory/
     dom_objects.js
     functions.js
     generals.js
     handlers.js
    modules/
     actions/
      set_actions_1.js
      set_actions_2.js
      set_default_action.js
      get_actions.js
     callbacks/
      sub_callbacks_1.js
      sub_callbacks_2.js
     defaults/
      set_defaults.js
     mdl_factory/
      //module specific files
     templates/
      sub_templates_1.js
      sub_templates_2.js

    index.js
&lt;/pre&gt;
&lt;h5 id=&quot;2-dom-objects-js-&quot;&gt;2 dom_objects.js:&lt;/h5&gt;
&lt;p&gt;That's the file where I collect my dom objects and it holds two functions, called getBaseObjects and getExtendedObjects. &lt;/p&gt;
&lt;pre style=&quot;background:#62686d; color:#fff&quot;&gt;
// assets/scripts/factory/dom_objects.js */
import * as FT from './functions.js';
export const getBaseObjects = async()=&amp;gt;{
  const map = new Map([['base_objects',{
    vvp: window.visualViewport,
    location_base: window.location.origin,
    body: document.body,
    wrap_ctn: await FT.elQuery('div.wrap.container', false, self.body),
    main_elem: await FT.elQuery('main', false, self.wrap_ctn), 
    actions_block: await FT.elQuery('.actions-block_ctn',false,self.wrap_ctn),
     //other base objects
  }]]);
  return map.get('base_objects');
}
 
export const getExtendedObjects = async (base_dom)=&amp;gt;{
  const {vvp,location_base,body,main_elem,actions_block} = base_dom;
  const map = new Map([['ext_objects',{
    vvp,location_base,body,main_elem,actions_block,
    main_sub_ctn: await FT.elQuery('div.main_sub_ctn',false,self.main_elem),
    //option 1 (not recommended), is just to show that you can group objects
    action_items:{
      action_item_1: await FT.elQuery('.action-item-1',false,self.actions_block),
      action_item_2: await FT.elQuery('.action-item-2',false,self.actions_block),
      action_item_3: await FT.elQuery('.action-item-3',false,self.actions_block),
      action_item_4: await FT.elQuery('.action-item-4',false,self.actions_block),
    },
    //option 2 (if your actions_block isn't going to change)
    action_items: await FT.elQuery('.action-item',true,self.actions_block),
    //option 3 (if your actions_block has live events and is going to change)
    action_items: await FT.getClassHelper('action-item',self.actions_block), 
    // uses 'getElementsByClassName'
    //-----------------------------
    //other ext objects
  }]]);
  return map.get('ext_objects');
}
&lt;/pre&gt;
&lt;h5 id=&quot;explanation-&quot;&gt;explanation:&lt;/h5&gt;
&lt;p&gt; I have created two functions here but for a reason.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;getBaseObjects&lt;/strong&gt; have values that are available as soon as the website loads and will never give an &lt;em&gt;undefined error&lt;/em&gt;:&lt;/p&gt;
&lt;pre style=&quot;background:#62686d; color:#fff&quot;&gt;
//assets/scripts/index.js
 import {getBaseObjects} from './factory/dom_objects.js';
 import {getActions} from './modules/actions/get_actions.js';
 (async ()=&amp;gt;{
   const base_elems = await getBaseObjects();
   await getActions(base_elems);
   //sure, there are more functions and classes that I call here.
 })();
&lt;/pre&gt;
&lt;h5 id=&quot;the-steps-here-&quot;&gt;The steps here:&lt;/h5&gt;
&lt;ol&gt;
&lt;li&gt;'getBaseObjects' is imported from 'dom_objects.js'.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;this function is called and applied to a const 'base_elems'.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;'getActions' is called from 'get_actions.js'.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;the values of step2 are passed as an arg to the 'getActions(base_elems)' function.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt; From here, the objects{keys:values} of 'base_elems' are available in the getActions function.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;getExtendedObjects&lt;/strong&gt; might have values that has not been created yet and could give an &lt;em&gt;undefined&lt;/em&gt; error if so? &lt;/p&gt;
&lt;pre style=&quot;background:#62686d; color:#fff&quot;&gt;
/** modules/actions/get_actions.js */
import {getExtendedObjects} from './../../factory/dom_objects.js';
import * as SA1 from './set_actions_1.js';
export const getActions = async(base_elems)=&amp;gt;{
 const ext_elems = await getExtendedObjects(base_elems);
 await setActions1(ext_elems);
}
&lt;/pre&gt;
&lt;h5 id=&quot;the-steps-here--1&quot;&gt;The steps here:&lt;/h5&gt;
&lt;ol&gt;
&lt;li&gt;'getExtendedObjects' is imported from 'dom_objects.js'.&lt;/li&gt;
&lt;li&gt;SA1.setActions1 is imported from 'set_actions_1.js'.&lt;/li&gt;
&lt;li&gt;function 'getActions' has been created together with a param 'base_elems'.&lt;/li&gt;
&lt;li&gt;within that function, 'getExtendedObjects' is called and args 'base_elems' are applied to it. Then this function is applied to a const 'ext_elems'.&lt;/li&gt;
&lt;li&gt;'SA1.setActions1' is called and has args 'ext_elems' applied to it.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;From here &lt;strong&gt;all objects&lt;/strong&gt; are available when called. &lt;/p&gt;
&lt;h4 id=&quot;in-a-real-situation-&quot;&gt;In a real situation.&lt;/h4&gt;
&lt;p&gt;In a real situation you only want the objects you need for a certain part of your webpage. &lt;/p&gt;
&lt;p&gt;Let say:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;'header' related objects for in the header.&lt;/li&gt;
&lt;li&gt;'main' related objects for in the main.&lt;/li&gt;
&lt;li&gt;'footer' related objects for in the footer.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The best way to do that is to create nested object groups in your Map(). &lt;/p&gt;
&lt;pre style=&quot;background:#62686d; color:#fff&quot;&gt;
const map = new Map([['ext_objects',{
  header_objects:{
    //header objects here
  },
  main_objects:{
    //main objects here
  },
  footer_objects:{
    //footer objects here
  },
})    
&lt;/pre&gt;
&lt;p&gt;Then in your 'getActions', you are doing this: &lt;/p&gt;
&lt;pre style=&quot;background:#62686d; color:#fff&quot;&gt;
export const getActions = async(base_elems)=&amp;gt;{
  const ext_elems = await getExtendedObjects(base_elems);
  //note: this is called 'destructuring', for objects it is {} and for arrays []
  const {header_objects,main_objects,footer_objects} = ext_elems;
  await headerAction(header_objects);
  await mainAction(main_objects);
  await footerAction(foot_objects);
}
&lt;/pre&gt;
&lt;p&gt;Now, each action get it's own objects.&lt;/p&gt;
&lt;p&gt;Enough for now!&lt;br&gt;
There is way more to say about it but I think this is enough for now! &lt;/p&gt;
&lt;h4 id=&quot;cheers-&quot;&gt;Cheers!&lt;/h4&gt;
</description>
<category>Objects</category>
<guid isPermaLink="true">https://coderlegion.com/5279/how-i-talk-with-the-dom</guid>
<pubDate>Wed, 10 Sep 2025 16:53:31 +0000</pubDate>
</item>
</channel>
</rss>