Static Import in JavaScript

If I have a module with an open member and I use it as

Module.Sub.member()

Then what is the best (if it exists) way to statically import a member into a local scope, such usingas in cpp or import staticin Java?

+5
source share
3 answers
var App = (function(app) {

    /* Dependencies */
    var Sound = app.Modules.Sound,        
        Input = app.Modules.IO.Input,
        ...

    /* Actual code using dependecies */
    ...    


})(App || {});

This solution has several advantages:

  • Indicated at the top of the module, it is clearly visible at a glance (supported code),

  • The usual benefits of importing are less typing, avoiding namespace conflicts, etc. (supported code)

  • Long ways to search for properties ( a.b.c.d) will be needed only once (performance),

  • , - (),

  • , .

+13

-

member = Module.Sub.member;

? , , " ", Java. , , .

, , import static.

+2

, , :

(function(member) {
    //Your code here will see member in this scope.
}) (Module.Sub.member);

Hope this helps.

+2
source

All Articles