Possible Duplicate:
How do you know when to return false from a jQuery function?
We all have some jquery code that looks like this:
$('#MySelector').somefunction(function () {
// do something
});
Is there a benefit/penalty for including a return statement and should we return true or false? For now, I’d say my code works fine without the return statements but I was wondering what would a return statement do in the context I described.
Thanks for your clarifications.
You’re passing the anonymous function as a parameter to
somefunction, so it really depends on whatsomefunctiondoes with that parameter. What really matters is whatsomefunctionreturns. For example, let’s say this is how it’s defined:So here we assign the return value of
fnParamto a variable and use that variable to determine actions withinsomefunction.somefunctionreturnsthisthough, which would be the jQuery object that it was passed (in your example$('#MySelector')). Since you returnthis,somefunctioncan chain with other jQuery methods like: