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

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

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 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