Javascript : the best way to create your own function(s) inside Jquery Ready function


Here is the best way to create function(s) inside JQuery ready :

jQuery.ready(function() {

     var myFunc1 = function() {
           // do some stuff here
     };

     var myFunc2 = function(param) { 
         alert(param);
         // do some stuff here 
     };

     myFunc1();
     myFunc2("Hello allien");

     // call it again!
     myFunc1();
     myFunc2("Hello allien");
});

And the bad way is shows below, every function will only calls once 

jQuery.ready(function() {

     function myFunc1() {
           // do some stuff here
     };

     function myFunc2(param) { 
         alert(param);
         // do some stuff here 
     };

     myFunc1();
     myFunc2("Hello allien");

     // call it again! do it work?
     myFunc1(); //error start from here!
     myFunc2("Hello allien");
});
At leat I tested it on Mozilla firefox 15.
:)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s