jQuery and function scope -


is this:

    ($.fn.myfunc = function() {        var dennis = function() { /*code */ }        $('#element').click(dennis);     })(); 

equivalent to:

    ($.fn.myfunc = function() {        $('#element').click(function() { /*code */ });     })(); 

if not, can please explain difference, , suggest better route take both performance, function reuse , clarity of reading.

thanks!

the difference former provides reference function.

thus, can this:

($.fn.myfunc = function() {    var dennis = function() { /*code */ }    $('#element').click(dennis);     dennis(); })(); 

which isn't possible latter.

this can useful. example, may want click manipulate part of page, want on page load. with:

$(function(){       var manipulatesomething = function() {         // stuff     };      // on click     $("#element").click(manipulatesomething);      // , right (document.ready)     manipulatesomething();  }); 

(aside: wouldn't call $("#element").click(); accomplish unless wanted click handlers on #element fire.)


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -