<title>js– scope problems</title> js– scope issues Table of Contents
- Variable declarations and function declarations
function F () { a = 3; equivalent to WINDOW.A = 3}f (); alert (a); 3functionF() { vara = 3;} f (); alert (a); Not defined
var t = 10; function F () { 3
var t = 10; function F () { vartTen
var a = 3; function F1 () { a = 4; function F2 () { a = 5; } F2 ();} F1 ();d ocument.write (a); 5
var a = 3; function F1 () { vara = 4; function F2 () { a = 5; } F2 ();} F1 ();d ocument.write (a); 3
Variable declarations and function declarations
For the parsing process of the declaration: Parameter –> variable –> function declaration
Rule 1:
For variable declarations
If there are arguments and declared variable names that are the same, they cannot be overwritten (no re-declaration of a variable), but there are assignments that override, and rules 1 and 2 only discuss variable declarations.
Rule 2:
for function declarations
Function declarations can override variable declarations and parameters
--For rule 1 function F (a) { vara; Alert (a) is not declared here ; 8}f (8); function F (a) { vara = 2; alert (a); 2}f (3);
// --For rule 2function F() {var a; alert (a);function a() {} alert (a);} f ();// outputs two a functions, stating that the original a declaration is overwritten by a function declarationfunction F() {var a=' Hello ';// At the end of the assignment, function A is overwritten with ' hello ', and the assignment is parsed after the variable declaration and function declaration analysis.alert (a);function a() {} alert (a);} f ();// output of two ' Hello 'function a(b) {alert (b);// First, assign a value of 1b =function() {// there is no function declaration, direct assignment, and then assign a value to a functionalert (b); } b ();} A (1);// The result is 1 and a function
JS--Scope problem