I am really confused as to why sometimes vars will not work when "var" is declared in front of them when used in a namespaced object. Isn't adding "var" in front of every variable the correct thing to do to keep it outside the global namespace?
Or would creating any var without declaring "var" first in my namespaced object, ensure of this, so I don't eed to worry about "var"?
Here's an example of my code:
MYNAME.DoStuff = {
initialize: function() {
var var1 = 'name'; //1
var2 = 'name'; //2
this.var3 = 'name'; //3
var $var4 = $('#' + name); //4
$var5 = $('#' + name); //5
this.$var6 = $('#' + name); //6
},
linkStuff: function() {
// then use the vars from the init above in here
}
}
MYNAME.DoStuff.initialize();
Can someone tell me which number (1, 2, or 3) is correct? Are there cases where I would use more than one or all? How about when I need to do DOM references with jQuery? Which way is correct (4, 5, or 6)?