I would like to find the easiest way to be sure about the scope of variables.
Seeing at next example (jsfiddle):
var foo = function() {
var bar = function() {
pub = "public";
var pri = "private";
alert(pub) // public
alert(pri) // private
};
// alert(pub) // pub not defined
bar();
alert(pub) // public
// alert(pri) // pri not defined
};
foo();
alert(pub) // public
// alert(pri) // pri not defined
Can I say that:
1.- All variable prefixed with var keyword will be visible inside its function and all their nested function?
2.- All variable without var keyword will be visible everywhere after (his function container) be executed?
var. If you intend a global, make it explicit. Run code in strict mode to ensure you haven't accidentally made an implicit global.