Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
JS - When to apply the module pattern

I have a module "badges.js", where I just have two methods: "increaseBadge" and "resetBadge".

//
// badges.js
//
function increaseBadge(badgeType, userId, value) {}

function resetBadge(badgeType, userId) {}

module.exports = { increaseBadge, resetBadge };

Does it have sense to apply the module pattern in order to offer a clean interface? Should I use an IIFE or just export an object?

I mean, this

const badges = Object.freeze(
  VALID_BADGES_TYPES.reduce(
    (acc, badgeType) => (
      (acc[badgeType] = {
        increase: (userId, value) =>
          increaseBadge(userId, badgeType, value),

        reset: (userId) =>
          resetBadge(userId, badgeType),
      }),
      acc
    ),
    {}
  )
);

function increaseBadge(badgeType, userId, value) {}

function resetBadge(badgeType, userId) {}

module.exports = badges;

VS encapsulating all the logic with an IIFE (does it have sens? Since I am already declaring all this related stuff in a module)

const badges = (() => {
  function increaseBadge(userId, badgeType, value) {}

  function resetBadge(userId, badgeType) {}

  return Object.freeze(
    VALID_BADGES_TYPES.reduce(
      (acc, badgeType) => (
        (acc[badgeType] = {
          increase: (userId, value) =>
            increaseUserBadge(userId, badgeType, value),

          reset: (userId) => resetUserBadge(userId, badgeType),
        }),
        acc
      ),
      {}
    )
  );
})();

module.exports = badges;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The point of the revealing module pattern is to create a bundle of logic without polluting the global scope with variables that are used internally.

CommonJS modules have their own scope already, so there is no point in applying the revealing module pattern inside a CommonJS module.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda