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

145
Visualizações
Creating a JS library with IIFE namespacing

If we don't want to use ES6 import nor any third party libraries (like require.js etc.) nor any package builder, what is the classical way to pack a library so that the user can use it with what seems to be a library-namespace?

index.html (from the perspective of the user of the library)

<canvas id="mycanvas"></canvas>
<script src="canvas.js"></script>
<script>
canvas.setup({"config1": true, "config2": false});    
var mycanvas1 = canvas.get("#mycanvas");
</script>

I sometimes see code similar to

// canvas.js
(function (window, something, else) {
    window.canvas.setup = function() { ... }    // how to export functions setup and get?
})(window, ..., ...);

How to do this IIFE properly to create this canvas.js library example?


Note: with ES6 import, we can package this way a library called canvas.js:

const canvas = {
    setup: config => { console.log("config"); },
    get: id => { console.log("get"); }
};
export default canvas;

and use it a index.html this way:

<canvas id="mycanvas"></canvas>
<script type="module">
import canvas from "./canvas.js";
canvas.setup({"config1": true, "config2": false});
var mycanvas1 = canvas.get("#mycanvas");
</script>

but in this question here I am curious about the pre-ES6 solution.

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

0

A solution seems to be:

// canvas.js
canvas = (function(window) {   // assign the result of the IIFE to a global var to make
                               // it available for the user of the lib
    var internal_variable;     // not accessible to the user of the lib
    var state;                 // this variable will be available to the user of the lib
    function setup(arg) {
        console.log("setup");
    }
    function get(arg) {
        console.log("get");    // + other lines using window object
    }
    return {setup: setup, get: get, state: state}; // important: here we choose what we 
                                                   // export as an object
})(window);

Now the user can use the library this way:

<canvas id="mycanvas"></canvas>
<script src="canvas.js"></script>   // object canvas is now available
<script>
canvas.setup({"config1": true, "config2": false});
var mycanvas1 = canvas.get("#mycanvas");
console.log(canvas.state);
</script>
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