How can I split a javascript application into multiple files?

后端 未结 5 1976
心在旅途
心在旅途 2020-12-04 11:19

I created a javascript application with all of the code in one file. The application has grown quite a bit and I think it\'s time to split it up into multiple files, but I\'

5条回答
  •  一个人的身影
    2020-12-04 11:40

    You can put the shared functions and shared modules on the myApp object so they don't pollute the global namespace, but can be accessed anywhere without being inside the same closure.

    myApp.moduleOne = function() {...}
    myApp.moduleTwo = function() {...}
    myApp.globalFunction = function() {...}
    

    Then, you can define them in any file and use them in any file.

    You could also just break the file up into multiple files, but require them to be included in a specific order that preserves your closure. If you're breaking up the files for practical editing reasons, but recombining and minimizing them for actual deployment, then this wouldn't cost you anything in terms of how you write code or how it's deployed, but would give you lots of smaller files for editing convenience.

提交回复
热议问题