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

210
Visualizações
is it possible to access variables and functions in one IIFE in another IIFE

Im currently studying to be a web developer, front and back end, and to help keep my code organized they suggest using IIFEs where necessary, if I were to use more than one IIFE could I call a function or variable from on IIFE in another IIFE? or is it only accessible within the IIFE its in? using Javascript by the way.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's only possible to access things across IIFEs if one of them deliberately exposes some of its parts globally. For example:

const myLibrary = (() => {
  // lots of code
  return {
    fn: () => console.log('running fn')
  };
})();

(() => {
  myLibrary.fn();
})();

or

(() => {
  // lots of code
  window.myLibrary = {
    fn: () => console.log('running fn')
  };
})();

(() => {
  myLibrary.fn();
})();

The usual way to solve this issue is to put your whole script into a single IIFE.

(() => {
  const myLibrary = {
    fn: () => console.log('running fn')
  };
  myLibrary.fn();
})();

For professional code, in anything other than toy snippets, I'd recommend using ES6 modules and a bundler like Webpack, which allows you to write code in separate files, import and export from other files as needed, and then puts everything together in a single ("bundled") script (composed of one huge IIFE) that you can put on your page.

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