.js file format

I was wondering if there is a good link on how to better format .js files?

Do you treat them like a class of files, or do you separate the functionality from the page that it processes? Are you creating .js functions that can be used on multiple pages, or are you writing a custom .js file for each page?

I'm just wondering if there is a formal way, and not just write a bunch of functions that may or may not be combined together for a specific page or set of pages. Right now I'm trying to group by page functionality, but my .js files are very customizable for this page, not sure if they can be used on another page.

:: UPDATE ::

Some of the answers have the same topic and mainly about what you plan to do with the project. Now the project is completely new for me, and the requirements are slow. Therefore, I build it on the pages. There may be cross functionalities or similar functions in the future. Most of the functionality is very specific to a particular page.

Is it wise to group AJAX calls into .js, but also have a custom .js page that contains all the highly customizable page functions.

+3
source share
7 answers

Typically, with javascript, you will want to reduce the number of individual javascript files that you specify on your pages to reduce HTTP requests.

+3
source

, , Rebecca Murphys jQuery js.

+3

, . ? js , .

? js .

+2

, , , nico. ( , - .) javascript http.

+1

, . <body> - .

-

my_app.page_handlers = {
   foo: function() { /* ... */ },
   bar: function() { /* ... */ }
   /* ... */
}

, , <body id="foo">, my_app.page_handlers.foo().

(, page_handlers.js), , .

Python. page_handlers.js page_handlers/foo.js page_handlers/bar.js. , , , .

+1

, .

, . , util. - ( ).

, , .

0

I don’t know if there is a standard for this, but maybe big projects like jQuery, Dojo, YUI library (Yahoo!) etc. can show you some good practices. I especially do all Object Oriented in JavaScript, so I use to separate each class in different js files, and after I use "cat" to put everything together, then YUI Compressor to minimize it. I apologize for this incomplete answer, I will also follow this thread!

0
source

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


All Articles