JavaScript One, no block-level scope in JavaScript
- A brace is scoped to a block-level scope, and block-level scopes exist in Java and C #
function Main () { if (1==1) { var name = ' seven '; } Console.log (name);} Output: Seven
Second, JavaScript uses function scope
- In JavaScript each function acts as a scope, outside of which the variables in the internal scope cannot be accessed
function Main () { var innervalue = ' Seven ';} Main (); Console.log (Innervalue); Error: Uncaught referenceerror:innervalue is not defined
Third, the scope chain of JavaScript
- Because each function in JavaScript acts as a scope, if a function nesting function occurs, the scope chain
XO = ' Alex '; function Func () { var xo = "Seven"; function inner () { var xo = ' Alvin '; Console.log (XO); } Inner ();} Func ();
- If there is a scope chain, then the search variable will appear in order, is based on the scope of the chain from the inside to the outside of the priority to find, step up to look up, not found to throw an exception
Iv. The scope chain of JavaScript was created before execution
XO = ' Alex '; function Func () { var xo = "Seven"; function inner () { console.log (XO); } return inner;} var ret = Func (); ret ();//output Result: Seven
The above code, before the function is called, the scope chain already exists:
The above code, before the function is called, the scope chain already exists:
- Global scope, Func function scope, inner function scope
When executing "ret ();", because its surrogate refers to the inner function, the scope chain of this function has been defined before execution: global scope, Func function scope, and inner function scope, so, when executing "ret ();", The variable is searched based on the existing chain of scopes.
XO = ' Alex '; function Func () { var xo = "Eirc"; function inner () { console.log (XO); } XO = ' seven '; return inner;} var ret = Func (); ret ();//output Result: Seven
The code above is the same as example one, and it also emphasizes that the scope chain already exists before the function is called:
- Global scope, Func function scope, inner function scope
At different time, the value of the XO variable in the FUNC scope has been reset from "Eric" to "seven" when executing "var ret = Func ();", so you can only find "seven" when you Execute "ret ()".
XO = ' Alex '; <br>function Bar () { console.log (XO);} function Func () { var xo = "Seven"; return Bar;} var ret = Func (); ret ();//output Result: Alex
The code above has created two scope chains before the function is executed:
- global scope, bar function scope
- Global scope, Func function scope
When performing "ret ();", the RET surrogate refers to the bar function, and the bar function's scope chain already exists: global scope--bar function scope, so execution will be based on the existing scope chain to find.
v. Declaration of Advance
- In JavaScript, if you do not create a variable and go directly to it, the error
Console.log (Xxoo);//Error: Uncaught Referenceerror:xxoo is not defined
- If you create a variable without assigning a value in JavaScript, the default assignment is undefined
var xxoo;console.log (Xxoo);//output: undefined
And if you write like this,
function Foo () { console.log (XO); var xo = ' Seven ';} Foo ();//output: undefined
The above code, not error, but output undefined, the reason is:javascript function before it is executed, it will declare all of the variables, without assigning value , so, equivalent to the above example, the function in the ' Precompiled ', the Var XO has been executed,so the above code is output undefined
Python
- Python can change variable scope code keywords: def, class, Lambda
- If statements, try exception captures, For/while loop statements do not involve variable scope changes, that is, variables in their code blocks, which are also accessible externally
def main (): if True: name = ' Seven ' print (name) main () #sevendef main (): inner = ' seven ' main () print (inner) #NameError: Name ' inner ' is not defined
- Python is also in the form of a scope chain, with the priority being to look inside and out from the scope
name = ' Alex ' def func (): name = ' Seven ' def Inner (): name = ' Eric ' print (name) inner () func ()
#eric
name = ' Alex ' def func (): name = ' Seven ' def Inner (): print (name) return Innerret = func () ret () #seven
- function is called before the scope chain has been created.
name = ' Alex ' def Bar (): print (name) def func (): name = ' Seven ' return Barret = func () ret () #alex
When an internal scope wants to modify the variables of an outer scope
- The modified variable is on the global scope and is used by the global Declaration
- The modified variable is on the nested scope, using the nonlocal declaration
#全局作用域count = 10def outer (): Global Count print (count) count = print (count) outer () # 10# 100# Nested Scope def outer (): count = ten def inner (): nonlocal count count = print (count) inner () Print (count) outer () # 20# 20
- Variable lookup order: scope local > Outer scope > Global scope in current module >python built-in scope
Python scopes and JavaScript scopes