Can you use multiple .JS files when developing a complex Javascript application?

前端 未结 4 1500
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 02:03

Coming from a C# background where every class is (best practices) stored in its own individual file, it makes development quite clean. I\'ve never written anything complex in Ja

4条回答
  •  遇见更好的自我
    2021-02-01 02:48

    There two possible ways. Personally, I would use a build tool to simplify working with multiple files.

    Using a build tool

    Grunt

    My favourite tool to keep up with complex js applications is grunt. With grunt you can develop in as many files as you want and use its plugins watch and concat to automatically concat them on save. You can do a lot more but this is the basic use case which may be helpful for you.

    Grunt requires nodejs and takes some time to setup. But once you are ready with your Gruntfile setup it really speeds up your development process.

    To make your project ready for production use you can also minify your scripts with some configuration and a single command.

    A lot of the major javascript libraries are using grunt, easily recognizable based on their Gruntfile: jQuery, AngularJS, Twitter Bootstrap etc.

    Grunt is also part of the development toolset yeoman.

    Brunch

    Brunch is another build tool which allows you to do similar things like grunt does.

    Loading only the needed files

    If you are developing a huge single page application and are concerned about the startup time of your application, one single file may not be the best solution. In this case you can use a javascript module loader.

    Require.js

    Therefor require.js is a goot fit. It allows you to only load the actual needed files on the current page. Though setting up require.js is a bit more work than setting up grunt.

提交回复
热议问题