I heard today that:
it is possible to access a local variable of a function since everything in JavaScript is global.
As far as I know, you can’t access a local variable from outside of the scope of the variable.
For example:
function f()
{
var myvar = "something";
}
myvar = "c"; // i'm not accessing myvar in f();
I also heard that it’s possible to use for(var i in window) to access myvar. I want to confirm it is not possible since I’m not the author of the language.
Updated:
I asked him a code snippet, and here’s what I have received.
var person = {
whoIs : function()
{
var name = "name";
return name;
}
};
var str = "TEST:\n";
for(var n in person)
{
str += n;
str += " = [" + person[n] + "]\n";
}
// perform regular exp. to get the value of name variable.
alert(str);
It’s not accessing the variable.. it’s simply printing how the function looks like…
That developer was wrong. Those two
myvarare different. The outside one is equivalent towindow.myvar, but the inside one is only inside thef.Edit: a very simple example: http://jsfiddle.net/mRkX3/
Edit 2:
A quote from the ECMAScript standard:
Found through http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting though that article is referencing a deadlink (live link: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf).