I read a lot about global variables as bad practice and I understand the reasons. Yet, in some cases it seems so much more easy. So this is my case and question.
I have some functions that animate (actually I use query.transit plugIn by Rico St. Cruz) a bunch of elements together like:
$element_1 = $("#element_1");
$element_2 = $("#element_2");
$element_3 = $("#element_3");
$element_4 = $("#element_4");
function animate_1()
{
// do something with every element
$element_1.transition( {…}, 100, 'easeOutSine' )
$element_2.transition( {…}, 150, 'easeOutExpo' )
$element_3.transition( {…}, 100, 'easeOutSine' )
$element_4.transition( {…}, 200, 'easeOutBack' )
}
function animate_2()
{
// do something with every element
$element_1.transition( {…}, 100, 'easeOutSine' )
$element_2.transition( {…}, 150, 'easeOutExpo' )
$element_3.transition( {…}, 100, 'easeOutSine' )
$element_4.transition( {…}, 200, 'easeOutBack' )
}
function animate_3()
{
// do something with every element
$element_1.transition( {…}, 100, 'easeOutSine' )
$element_2.transition( {…}, 150, 'easeOutExpo' )
$element_3.transition( {…}, 100, 'easeOutSine' )
$element_4.transition( {…}, 200, 'easeOutBack' )
}
This is my solution for now and it works fine, yet, it is using the elements as global variables and my .js file is imbedded after the end of my pages. If global variables are bad practice how would it be preferable to do? I know that I could define the selectors in each function thus repeating the identical declaration 4 times which seems silly to me. I am looking forward to your feedback on this generell issue.