When to assign functions to variable, using jquery
so I've been messing around with some Jquery Ajax promises/deffers etc...
and i've come across something I don't completely understand, not strictly
related to the Jquery Ajax.
I have always declared and called functions like so:
function foo1() { //sets function
alert('foo1');
}
foo1(); //calls function
But it seems the more I see different code a lot of people are declaring
functions like the following, I just copied and pasted an example I saw so
I would't miss anything:
var promise = $.ajax({
url: "/myServerScript"
});
promise.done(myStopAnimationFunction);
I understand what the above does, just an example.
The question is, is it better to assign functions to variables? What are
the pros/cons, and in what situations is this way used? At what point in
this code is the actual function called. Does
promise.done(myStopAnimationFunction);
call both the ajax function, and then the callback, or just the callback?
Thanks
No comments:
Post a Comment