I currently have all my script's required elements cached in a global object similar to this:
var MainObject={
$El1 : $('#element1'),
$El2 : $('#element2')
};
Inside my methods I can then just access the object directly.
method1:function(){
MainObject.$El1 // DO SOMETHING WITH THIS ELEMENT
}, ...
So, I have 2 questions.
I've read that local variables are the fastest. would it be better to write my methods like so?
method1:function(){
var $El1=MainObject.$El1;
$El1 // DO SOMETHING WITH THIS ELEMENT
}, ...
and if so...
If I have many Methods in my script that reference these elements (which can quickly turn into quite a few lines) what would be the best way to condense them?
method1:function(){
var $El1=MainObject.$El1,
$El2=MainObject.$El1,
$El3=MainObject.$El1,
$El4=MainObject.$El1;
$El1 // DO SOMETHING WITH THIS ELEMENT
},
method2:function(){
var $El1=MainObject.$El1,
$El2=MainObject.$El1,
$El3=MainObject.$El1,
$El4=MainObject.$El1;
$El1 // DO SOMETHING WITH THIS ELEMENT
},
method3:function(){
var $El1=MainObject.$El1,
$El2=MainObject.$El1,
$El3=MainObject.$El1,
$El4=MainObject.$El1;
$El1 // DO SOMETHING WITH THIS ELEMENT
},
Thanks!