1. Lexical Scope
Function C1 (){ Return D1 ++ }
Function A1 (){ VaR D1 = 0 ; Return C1 ();}
Console.info (A1 ())
// D1 undefined C1 is defined externally in A1
// 1 error. CC not found in variable object
Alert (cc)
// 2 undefined. The parameter CC is found in the variable object, but no value is assigned.
VaR Xx = Function (Cc ){
Alert (cc)
}()
// 3 error. The variable object sets the parameter o = undefined.
VaR Xx = Function (O ){
Alert (O.)
}()
// 4 error. Variable object declares o = undefined
Alert (O.)
VaR O = {}
// 6 undefined. After the execution, O is the object.
Alert (O.)
// 7 error. Variable object declares o = undefined
VaR O;
Alert (O.)
2. Scope chain parameter> function declaration> variable Declaration
// If the parameter has been declared but has no value, it is undefined.
// However, if the parameter is not declared, it will be searched up.
// 1 undefined. In a function, the variable object parameter A = undefined, Which is preferentially used.
VaR A22 = 0 ;
VaR Xx = Function (A22) {alert (A22 )}()
// 2 parameter a2 = undefined. B2 cannot be found, and then it finds = 4.
VaR D2 = Function (A2 ){
VaR A2 = 3 , B2 = 4 ;
VaR C2 = Function (A2 ){
Alert (A2)
Alert (B2)
}()
}()
3. Sequence
Console.info ( Typeof Ss3) // Function
VaR Ss3 = 2
Function Ss3 () {alert ( " OK " )}
Console.info ( Typeof Ss3) // Number
// The first output is function because the followingCodeNot executed. In the variable object, the function SS declares that the type is function, and the declaration of the variable SS cannot overwrite the function ss.
// The second output is the number, because the following code has been executed and the SS is assigned a value of 2,
// See http://www.javaeye.com/topic/812668 if you do not understand
Four block-level declarations
// FF = 1 Ie = 2. FF can be declared in block level?
Function F () {alert ( 1 )};
F ();
If ( 1 ){ Function F () {alert ( 2 )}}
---------------------------------------
For ( VaR I4 = 0 ; I4 < 5 ; I4 ++ ){ If (I4 = 3 ) Break }
Alert (I4)
// I4 is a global scope
5. Anonymous and naming Functions
// Solution to the function declaration in block-level elements, so that both ff and IE are first
---
VaR Foo;
If ( True ){
Foo = Function (){
Return ' First ' ;
}
}
Else {
Foo = Function (){
Return ' Second ' ;
}
}
Alert (FOO ())