5 ms·
Interesting. I just started this, and came back here to post this. The third question - "Global Variables Intro" in the "beginner" (one by http://madebyknight.
by raju 14y ago
Interesting. I just started this, and came back here to post this.
The third question - "Global Variables Intro" in the "beginner" (one by http://madebyknight.com/javascript-scope/ http://madebyknight.com/javascript-scope/) is not quite right.
function() {
a = 3;
alert(a);
}
is not the same as
var a;
function() {
a = 3;
alert(a);
}
Whenever you don't declare a variable with a var, it does not become a global variable, rather, it becomes a property of the global (or host) object.
The main difference here is that you can delete a property -
delete window.a;
But you can't delete 'a' if it were defined by var.
In the same light, the following statement (IMO) is incorrect.
When Javascript comes across a global variable, it needs somewhere to put it. It needs somewhere that everywhere else on the page can access. So, it uses the window object.
Still working through the tutorial ...
Edit - Copy pasted the wrong sentence from the quiz. Sorry
- bolshchikov 14y agoGlobal variables are in fact properties of the global object. In web pages the global object is window, so you can set and access global variables using the window.variable syntax. https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals?redirectlocale=en-US&redirectslug=Core_JavaScript_1.5_Guide%2FValues%2C_Variables%2C_and_Literals#Variables https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Va...