Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda