DEV Community

Aad Pouw
Aad Pouw

Posted on

Why I prefer my own 'new Map()' based createObjects() function over 'new Map()' *.set().

It's about, why I prefer this function over using 'new Map()' *.set()?
First the code:

export async function createObjects(...args){
  const [map_object = null, map_entries = null] = args;
  if(map_object !== null && map_entries !== null){
    const map = new Map([[map_object,map_entries]]);
    return map.get(map_object);
  }
  return null;
};
Enter fullscreen mode Exit fullscreen mode

The advances of this function over using
*.set('key1', 'value1');
*.set('key2', 'value2');

It can be used in the same way as using an ordinary object structure, doing the same thing as 'set' and is much shorter;

  const foo = {key1: val1,key2: val2,};
  const bar = await FT.createObjects('bar_obj',{key1: val1,key2: val2,});
Enter fullscreen mode Exit fullscreen mode

or

  const foo = {};
  foo.key1 = val1;
  foo.key2 = val2;

  // 'FT' is a namespace and is from 'import * as FT from "...js"'!
  const bar = await FT.createObjects('bar_obj',{});
  bar.key1 = val1;
  bar.key2 = val2;

  bar['key1'] = val1;
  bar['key2'] = val2;
Enter fullscreen mode Exit fullscreen mode

Note:

It's used in:
My recently created '*ModuleEditer' and wherover more at a later stage.

My present rebuild of my github page and wherover more at a later stage too.

  • Also, I do this rebuild on my localhost and I will replace my present GH page, when the time is right for it.

That's it!

Top comments (0)