Closed
Bug 1360102
Opened 8 years ago
Closed 5 years ago
About the global property with "var" in MDN.
Categories
(Developer Documentation Graveyard :: JavaScript, enhancement, P5)
Tracking
(Not tracked)
RESOLVED
WONTFIX
People
(Reporter: roachbody, Unassigned)
References
()
Details
:: Developer Documentation Request
Request Type: Correction
Gecko Version: unspecified
Technical Contact:
:: Details
Hi, MDN team,
According to MDN's article about introducing the "variable statement".
The paragraph before first sample code says:
"1. Declared variables are constrained in the execution context in which they are declared. Undeclared variables are always global."
But accrording to my tests,
I think that's not quite right,
The fact might is,
No matter using var or let,
the undeclared varaibles would be a global variable until it hoisting to the scope where is a declared & same name variable exsist.
So, it's restrictive global, not totally global.
Please check the code below:
var x = 0; // replaced by x in t1.
var y = 0; // no replacement happened.
var z = 0; // replaced by z in t3.
(function t1() {
x = 1; // undeclared, would go upward and fiding declared & same name variable.
var y = 1; // replaced by y in t2.
var w = 1; // replaced by w in t3.
(function t2() {
var x = 2; // it's local var, wouldn't, go upwrad.
y = 2; // undeclared, would go upward.
(function t3(){
var x = 3; // it's local var, wouldn't, go upwrad.
z = 3; // undeclared, would go upward.
w = 3; // undeclared, would go upward.
}());
document.write("========= t2 ======<br/>");
document.write("x: " + x + "<br/>"); // 2
document.write("y: " + y + "<br/>"); // 2
document.write("z: " + z + "<br/>"); // 3
document.write("w: " + w + "<br/>"); // 3
}());
document.write("========= t1 ======<br/>");
document.write("x: " + x + "<br/>"); // 1
document.write("y: " + y + "<br/>"); // 2
document.write("z: " + z + "<br/>"); // 3
document.write("w: " + w + "<br/>"); // 3
}());
document.write("========= global ======<br/>");
document.write("x: " + x + "<br/>"); // 1
document.write("y: " + y + "<br/>"); // 0
document.write("z: " + z + "<br/>"); // 3
document.write("w: " + w + "<br/>"); // w would be "not defined", because it's undeclared in global scope.
So, please correct me if I misunderstood anything.
Thanks a lot.
Wish you guys have a good day. ^^
Comment 1•5 years ago
|
||
MDN Web Docs' bug reporting has now moved to GitHub. From now on, please file content bugs at https://github.com/mdn/sprints/issues/ and platform bugs at https://github.com/mdn/kuma/issues/.
Status: UNCONFIRMED → RESOLVED
Closed: 5 years ago
Resolution: --- → WONTFIX
You need to log in
before you can comment on or make changes to this bug.
Description
•