Disadvantages of splitting JavaScript codes?

I have a page with many event handlers. Now the code has reached 1000+ lines of codes, and I'm starting to have difficulty reading codes. Now I plan to split the codes into different files. My question is, are there any flaws in splitting JS codes into different files?

+6
source share
6 answers

Disdvantages?

  • another HTTP request. developers report that files should be compressed and have as few files as possible for optimization. the fewer files you request, the faster (since there are fewer round trips)

  • if you use a text editor with autocomplete, some of these editors will not collect material from another file for autocomplete. this can be a problem if you do not memoize the functions that you have.

  • If one of these interdependent files does not load, your application will break.

+3
source

The best practice is to create relatively small files for development purposes, where each file contains a module of functionality that is all connected (which is most effective for developing / debugging / editing / source control. You can give each file a meaningful name that describes what is in it. Each these files can be managed separately (their own version history in your version control system, check / check, etc.). It is often easier to open several tabs with separate files in your editor than the priests Use bookmarks to navigate between different places in one large file, etc.

Then, when you deploy the application, you use a tool (for example, closing Google or YUI Compressor) to minimize and merge all your smaller files into a single deployment file. This preserves the advantages of developing smaller files that contain related code, while retaining the advantage of deploying that most / all of the code is in one larger external javascript file that can be loaded into a single HTTP request and can be cached very effective.

+4
source

The downside would be the added complexity of making sure you turn everything on correctly. But 1000 lines become bulky. Here is a related page about calling JavaScript objects in separate files

+3
source

In addition to my comment.

Use t4 templates here , or you can use c # to write javascript script #

on modern platforms, developers do not write javascript directly, for example

  • in Rails using CoffeScript
  • in python using one of (py2js, pajamas, google V8, skulpt)
  • in c # using script #
+2
source

Here is a bolg post about JSminify and similar tools for use with VisualStudio and build automation: http://encosia.com/automatically-minify-and-combine-javascript-in-visual-studio/

+2
source

Having separate code in different files helps you maintain it. I suggest you divide it into modules compatible with CommonJS, if you work in a browser environment, you can use RequireJS to download them. You can also use the utility provided to create a single file when you deploy your website / webapp to optimize http requests.

+1
source

Source: https://habr.com/ru/post/911718/


All Articles