Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!