Jquery | Function inside same function. It bad practice?
javascript, jquery
Solution
There is no gain to be made with your method. Plus you are using the jQuery fadeTo function. There is nothing wrong with what you did, just no gain. You could save work with such a technique if for example you had less arguments in your custom function:
function fade_to(div, after_fade) {
$(div).fadeTo(3000, 1, after_fade);
}
fade_to('#div', function(){ fade_to('#another_div', $.noop); });
This would actually save you work by preventing you from having to enter speed and opacity arguments. You could also curry it like this
function Fade_to(speed, opacity){
return function(div, callback){
$(div).fadeTo(speed, opacity, callback);
}
}
Then you could make argument saving functions on the fly like
var fade_to_foo = Fade_to(3000, 1);
fade_to_foo('#div', function(){ fade_to_foo('#another_div'); });
Otherwise there is no reason not to just write it the jQuery way
$('#div').fadeTo(3000, 1, function(){ $('#another_div').fadeTo(3000, 1); });
Problem
I am using this function for easiness, as I am going to use fadeTo a lot: ``` function fade_to(div, speed, opacity, after_fade) { $(div).fadeTo(speed, opacity, after_fade); } ``` Then I am calling the same function for after_fade parameter: ``` fade_to('#div', 3000, 1, function() { fade_to('#another_div', 3000, 1)}); ``` Is that a bad thing to do? Will I have speed/smoothness issues? Is it better to just use jQuery's default fadeTo function? Thanks!