vendredi 13 avril 2018

Is wrapping map(or dictionary) with function a good idea?

When using map,I usually think that whether I should encapsulate it or not.

If I encapsulate it,there will be less global variables. Isn't it?

But when I try to write,it looks stupid.

Could somebody tell me which one is better,or more functional?

Before refactor:

//The map
const handles = {
  'a' : () => {
    console.log('a')
  }
}
//Use it
handles['a']()

After refactor:

function handle_fn(){
  //The map
  const handles = {
    'a' : () => {
      console.log('a')
    }
  }

  return (name) => {
    return handles[name]
  }
}

const handle = handle_fn()

//Use it
handle('a')

Aucun commentaire:

Enregistrer un commentaire