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

164
Visualizações
Use function wrapped in IIFE of js script A inside IIFE of js script B without global exposure

Privatization of functions + variables is pretty important in the current js project I'm developing. Consequence of this is that every script wraps its content inside an IIFE. What I'm wondering now is, to go a step further, is it actually possible in js, using import statements or similar, to do the following:

// contents of script A
(function(){

  function secretFunction() {
  }

}());
// contents of script B
(function(){

  // var AsFunction = // secretFunction of script A;

  // AsFunction();

}());

Without exposing secretFunction() to the global scope (which is the thing I was doing so far to reach what I want) .. ?

My thought was just to be able to use the functions of the different scripts within each other, without exposing them to the global scope, hence the 'risk' of them being modified from an external source is even further reduced. Like this, I would not need to attach functions as secretFunction() to the global scope to make them available in other scripts; hence I could use functions from within foreign IIFE scopes within other IIFE scopes. Is something like this possible in js?

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

0

So just in case anyone faced this problem and you were in the same situation as me: You've developed an entire js - framework for a business based on IIFE structure, and changing all of that to js - modular's logic would be interesting to do, but you have no time to do so.

In this case, you could also use an approach which exposes your function to the global scope to pass it from script A to script B, while any modifications applied to that function in the global scope would nevertheless still not affect the function when you use it in script B. Consider the following example to clearly see what I mean:

CONTENT OF SCRIPT A

(function(){
  window.sample = {};
  window.sample.test = function(){ console.log("initial"); };
}());

window.sample.test();
// This will log "initial"

CONTENT OF SCRIPT B

(function(){

  // Make a copy of the function of script A passed via global scope
  var copy = window.sample.test;
  copy();
  // This will log "initial"

  // Modify the global function passed to this script
  window.sample.test = function(){ console.log("modified"); };

  window.sample.test();
  // This will log "modified"

  copy();
  // This will still log "initial", although the global function, which is
  // the source of this copy, has been modified. The variable copy remains
  // unaffected by this, because the variable stores the value to what it is
  // assigned, as js always passes variables by value and not by reference.

}());
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