if I have a function defined earlier, do I need to include parenthesis when specifying that it should be used for a success callback?
what would be the difference if I did?
as in
function fish_food(){//do something}
$.ajax({
url: '/',
success: fish_food
});
or
$.ajax({
url: '/',
success: fish_food()
});
fish_foodon its own (without parens) acts as a reference to the function object. It allows you to pass the reference to the function around to be invoked at some later date.fish_food()(with parens) is a function invocation expression, which causes the function to be executed. The function code is evaluated and run with a value optionally being returned.With the AJAX code you supplied (and all async JavaScript involving callbacks) you want to use the
fish_foodversion (without parens). This passes the AJAX code a reference to your success function, to be executed asynchronously once the AJAX code has completed its round trip to the server and back.