jquery - Wrapping Events in Anonymous Functions - Javascript -


i've been reading on anonymous javascript functions , have general question using them event listeners via jquery.

i have following event listener stuff upon submission of form.

if(sky === 'blue') {     $('#my-form').on('submit', function(){         //do stuff         $('#my-div').show();     }) } 

would make difference (i.e., pros & cons, things go wrong, etc.) if declare as-is in javascript file or should wrapping in anonymous function so

 $(function() {     if(sky === 'blue') {     //do stuff  } 

any input appreciated, thanks!

if you're not declaring vars or functions and make sure code execute after dom ready doesn't make difference.

but common experience maybe sure premises true now, someday break 1 of them, you'll propably have problem (a leak, conflict, whatever).

as advice, tell wrap code immediately-invoked function expression (iife).


Comments