-2

I have created a class/library. and I used the new class syntax with a constructor and its methods, now what? what should I put in it for using that in a external JS file?

I have something like this

class LoadBalancer {
    constructor() {/*...*/}
    method1(...bla) {/*...*/}
}

Should I put at the top a use strict ?

'use strict';

Maybe put my class inside a Self-Executing Anonymous Functions? or use a export with my class name?

I have seen many .js files that have a syntax like this:

(function (root, factory) {
    if(typeof exports === 'object' && typeof module === 'object')
        module.exports = factory();
    else if(typeof define === 'function' && define.amd)
        define("EventBus", [], factory);
    else if(typeof exports === 'object')
        exports["EventBus"] = factory();
    else
        root["EventBus"] = factory();
})(this, function() { /*....
    their functions and libraries declaration goes here
    .....*/ 
    return whatEverNewFunc();
});

But I do not know what it means and what happens if you just do not use them or what are the advantages, what if I just place my class LoadBalancer {/*...*/}in a loadBalancer.js file, and thats it.

Fernando Carvajal
  • 1,869
  • 20
  • 19
  • Since you're speaking of a webWorker, is it a correct assumption that you're only targeting running in a browser? The usual point of enclosing code in an IIFE (immediately invoked function expression) is to prevent polluting the global namespace with your own variables and prevent conflict with others and to secure your own top level variables where they can't be accessed by outside code. In a browser, it is often a good thing. In node.js it is not needed because modules are already enclosed in their own function. – jfriend00 Jan 17 '18 at 17:31
  • The WebWorker has nothing to do in what I was asking, I have all my code inside a class declaration, I asked for the exports syntax too. This is my js [code](https://github.com/fercarvo/GenericWebWorker/blob/master/genericWebWorker.js) – Fernando Carvajal Jan 17 '18 at 17:43
  • What platforms you're targeting to run on has EVERYTHING to do with what is required to publish a module. That's why I'm asking. For node.js, you would publish as a module that exports its interface. For the browser without ES6, you would probably publish as a script that be included on its own and adds its interface to the global namespace or you'd require ES6 support and define it as an ES6 module. The last code block you show is about platform detection. – jfriend00 Jan 17 '18 at 17:45

1 Answers1

0

Yes, I'd recommend to write an ES6 module with an export statement for your class (and possibly other things that you want to export). You don't even need to put "use strict";, it's implicit in ES6 modules.

Release your library either as an ES6 module (for any modern environment or bundlers or transpilers), or transpile it yourself and release a version of it with the UniversalModuleDefinition style that you witnessed - the transpiler can add this for you. You may of course also release both versions.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375