-1

Working on MDN classes concept, I'd like to check if a class exits (not object nor function) in order to load it dynamically and redefine it if it is.

I tried many way (typeOf, instanceof...) unsuccessfully ...

    Class APP{
[...]
    registerModule( moduleName, force ) 
    {
        if( this.modules[ moduleName ] === undefined || force )
        {
console.log( "loading "+moduleName+"/js/"+moduleName+"Module.js ..." );
            //~ TODO : Add a test if class already loaded and if we have to 'unload' or re-define it
            this.loadScript( moduleName+"/js/"+moduleName+"Module.js", function(){
                APP.modules[moduleName] = eval( "new "+moduleName+"()" );
            });
        }else{
            alert( "Moule '"+moduleName+"' already declared !");
        }
    }
}
Glaubule
  • 169
  • 1
  • 7
  • A class _is_ a function. So `typeof SomeClass === "function"` would be the first step. – Sebastian Simon Jan 22 '18 at 13:21
  • 1
    How may you *not* know whether a class is defined? You need to get a handle on the class first, typically these days through `import Foo from 'bar'` or something along those lines. In that case you can be pretty sure the class exists. I'd hope you're not important something of which you have no idea what it is. – deceze Jan 22 '18 at 13:25
  • Can we see what you tried? – epascarello Jan 22 '18 at 13:30
  • If your classes are actually defined as `class Foo {}`, then you cannot even attempt to load the same class twice, it will result in an error. So "unloading" a class won't work. – deceze Jan 22 '18 at 13:53
  • That's my problem ! – Glaubule Jan 22 '18 at 13:55
  • Why may you be getting into a situation in which you're loading a class a second time?! – deceze Jan 22 '18 at 13:57
  • My APP loads modules when a user choose this module (button) but the module can already be loaded. Reloading it can be usefull if it has been updated – Glaubule Jan 22 '18 at 13:59
  • 1
    Then your loader should keep track of what it has already loaded and not attempt to load it a second time. – deceze Jan 22 '18 at 13:59
  • This notation was introduced in ECMAScript 2015 (ES6) Please read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/class – Glaubule Jul 11 '18 at 14:37

1 Answers1

0

OK, found a way :

class APP{
    constructor() {
        this.modules = [];
    }

    registerModule( moduleName, force ) 
    {
        if( this.modules[ moduleName ] === undefined || force )
        {
            if( this.modules[ moduleName ] === undefined )
            {
                this.loadScript( moduleName+"/js/"+moduleName+"Module.js", function(){
                    APP.modules[moduleName] = eval( "new "+moduleName+"()" );
                });
            }else{
                    APP.modules[moduleName] = eval( "new "+moduleName+"()" );
            }
        }else{
            alert( "Le module '"+moduleName+"' est déjà déclaré !");
        }
    }
}

Can be called this way :

APP.registerModule( 'Batch' ,true)

Class is instanciated at first load. Object is overwritten on new module registration, based on the same class definition. Class cannot be redefined (too bad). Thanks for your comments !

Glaubule
  • 169
  • 1
  • 7