javascript - Is this a smart way to organize code in jQuery? -


For the longest time I was writing all my code in this way ...

  $ (document) .ready (function () {$ (. Show_module '). Click (function () {}); ...});   

By putting all kinds of click handlers there but recently a new way of working was introduced and I would like to feel a great way to think about it if I want to feel. Was there.

The idea is to put all tasks in related tasks and then the document has minimum code. Above top

For example ...

  $ (document) .ready (function () {page_handler (); // follow other functions}); Function page_handler () {$ ('. Show_module'). Click (function () {}); ...}   

This allows the organization of related handlers in the work and uses the document. In the form of more initially

I know in javascript functions and variables 'hoisted' before starting the code

  do_something (); Function do_something () {}   

works for this reason, because the function is really ready to use before () is actually called, even if it is from the actual function definition First appears.

I was thinking that the same thing happens here and how it works, this is the idea / method.

This will show all your handlers in the global ( window ) radius, from which Conflicts can happen.

I like to do this ...

  (function ($) {// whatever you do here will not be attached to the window. A; // If you have something to be global then set it explicitly in the window.) {...} // We can also use $ for jQuery here, even if we have jQuery.noConflict ( ) $ ('Body') is used ...}) (jQuery);    

Comments