Best way to write javascript / jQuery

I'm not new to web design, but I'm just wondering if you guys have any advice for me:

Whenever I write js or jQuery, I write all the code like this in <head>on my page:

<script>
$(document).ready(function() {

   //All my functions and animations goes here…

});
</script>

Now, when I browse the web, I see that most people have their functions and animations in a separate .js file. And they also often create classes.

How it works? What is the advantage of creating classes and your functions in a separate file?

Thanks VG

+5
source share
5 answers

Benefits of placing your javascript in a separate file:

  • . javascript , .
  • . javascript, . JS , , JS .
  • HTML. . HTML, javascript, .

. JS , JS , JS .

/ :

  • . , . - , , , javascript.
  • . - , , , , . , , ( ).
  • - , , .
  • . , , , , .
  • . - , , .
  • . - , , .
  • . - .

FYI, - - ( javascript ). , , . , - C, . javascript - , - javascript ( ), ( , DOM ..).

+6

javascript- , ( , HTML) . . CSS. Styling . . Javascript , . , . , . , .

, . javascript, , , .

+4

, , . .

, ... , ! , - , . PHP .

+1

quite a few differences and best practices. But, for example, you want the jquery function to be in another file, you can write the code once. In addition, it will increase the speed of your website and will only download content once.

0
source

In addition, the implementation of js Object classes helps to avoid a collision of the namespace with the names of variables and functions ... in general, in my opinion, good practice.

0
source

All Articles