Approaches to modular client-side Javascript without namespace pollution

后端 未结 8 1281
借酒劲吻你
借酒劲吻你 2021-02-02 02:14

I\'m writing client-side code and would like to write multiple, modular JS files that can interact while preventing global namespace pollution.

index.html



        
8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 02:38

    I strongly suggest you try a build tool.

    Build tools will allow you to have different files (even in different folders) when developing, and concatenating them at the end for debugging, testing or production. Even better, you won't need to add a library to your project, the build tool resides in different files and are not included in your release version.

    I use GruntJS, and basically it works like this. Suppose you have your util.js and index.js (which needs the helper object to be defined), both inside a js directory. You can develop both separately, and then concatenate both to an app.js file in the dist directory that will be loaded by your html. In Grunt you can specify something like:

    concat: {
        app: {
            src: ['js/util.js', 'js/index.js'],
            dest: 'dist/app.js'
        }
    }
    

    Which will automatically create the concatenation of the files. Additionally, you can minify them, lint them, and make any process you want to them too. You can also have them in completely different directories and still end up with one file packaged with your code in the right order. You can even trigger the process every time you save a file to save time.

    At the end, from HTML, you would only have to reference one file:

    
    

    Adding a file that resides in a different directory is very easy:

    concat: {
        app: {
            src: ['js/util.js', 'js/index.js', 'js/helpers/date/whatever.js'],
            dest: 'dist/app.js'
        }
    } 
    

    And your html will still only reference one file.

    Some other available tools that do the same are Brunch and Yeoman.

    -------- EDIT -----------

    Require JS (and some alternatives, such as Head JS) is a very popular AMD (Asynchronous Module Definition) which allows to simply specify dependencies. A build tool (e.g., Grunt) on the other hand, allows managing files and adding more functionalities without relying on an external library. On some occasions you can even use both.

    I think having the file dependencies / directory issues / build process separated from your code is the way to go. With build tools you have a clear view of your code and a completely separate place where you specify what to do with the files. It also provides a very scalable architecture, because it can work through structure changes or future needs (such as including LESS or CoffeeScript files).

    One last point, having a single file in production also means less HTTP overhead. Remember that minimizing the number of calls to the server is important. Having multiple files is very inefficient.

    Finally, this is a great article on AMD tools s build tools, worth a read.

提交回复
热议问题