Examples of JavaScript scopes and javascript examples
Scope is one of the most important concepts of JavaScript. To learn JavaScript well, you need to understand the working principles of JavaScript scopes and scope chains. This article introduces the JavaScript scope example in detail, hoping to help you better learn JavaScript.
Any programming language has the concept of scope. Simply put, scope is the accessible scope of variables and functions, that is, scope controls the visibility and lifecycle of variables and functions. In JavaScript, variables have two scopes: global scope and local scope.
I. No block-level scope in JavaScript
There is a block-level scope in Java or C #, that is, braces are also a scope.
Public static void main () {if (1 = 1) {String name = "seven";} System. out. println (name) ;}// public static void Main () {if (1 = 1) {string name = "seven" ;}console. writeLine (name);} // Error
No block-level scope in JavaScript
Function Main () {if (1 = 1) {var name = 'seven';} console. log (name);} // output: seven
Ii. JavaScript uses function scopes
In JavaScript, each function acts as a scope and variables in the internal scope cannot be accessed externally.
Function Main () {var innerValue = 'seven';} Main (); console. log (innerValue); // error: Uncaught ReferenceError: innerValue is not defined
Iii. JavaScript scope chain
Since each function in JavaScript acts as a scope, if a function is nested, a scope chain will appear.
xo = 'alex';function Func(){var xo = "seven";function inner(){var xo = 'alvin'; console.log(xo);}inner();}Func();
For example, the code above shows a three-scope chain. If there is a scope chain, the order of variables will appear. For the above example:
When you execute console. log (xo), the search order is to search for the priority from the inner to the outer of the scope chain. If the inner layer does not exist, it will gradually look up until no exception is found and an exception is thrown.
4. the scope chain of JavaScript has been created before execution.
The JavaScript scope has been created before it is executed. You only need to search for it according to the scope chain when you execute it later.
Example 1:
Xo = 'Alex '; function Func () {var xo = "seven"; function inner () {console. log (xo) ;}return inner;} var ret = Func (); ret (); // output result: seven
The code above indicates that the scope chain already exists before the function is called:
• Global scope-> Func function scope-> inner function Scope
When you execute [ret ();], because it refers to the inner function, the scope chain of this function has been defined before execution: global scope-> Func function scope-> inner function scope. Therefore, when you execute [ret ();], the variable will be searched based on the existing scope chain.
Example 2:
Xo = 'Alex '; function Func () {var xo = "eirc"; function inner () {console. log (xo);} xo = 'seven'; return inner;} var ret = Func (); ret (); // output result: seven
The above code serves the same purpose as Example 1. It also emphasizes that the scope chain already exists before the function is called:
• Global scope-> Func function scope-> inner function Scope
When [var ret = Func ();] is executed, the value of the xo variable in the Func scope has been reset from "eric" to "seven ", therefore, when you run "ret ();", you can only find "seven ".
Example 3:
Xo = 'Alex '; <br> function Bar () {console. log (xo) ;}function Func () {var xo = "seven"; return Bar ;}var ret = Func (); ret (); // output result: alex
The above code has created two scope chains before the function is executed:
• Global scope-> Bar function Scope
• Global scope-> Func function Scope
When you run [ret ();], ret refers to the Bar function, and the Bar function's scope chain already exists: global scope-> Bar function scope. Therefore, the execution will be performed based on the existing scope chain.
V. advance declaration
If you do not create a variable in JavaScript and use it directly, an error is returned:
Console. log (xxoo); // error: Uncaught ReferenceError: xxoo is not defined
In JavaScript, if a value is created without a value assignment, the value is undefined, for example:
Var xxoo; console. log (xxoo); // output: undefined
If this is the case in the function:
Function Foo () {console. log (xo); var xo = 'seven';} Foo (); // output: undefined
The above code outputs undefined instead of an error because the JavaScript function declares all the variables without assigning values before execution. Therefore, it is equivalent that in the above example, when the function is pre-compiled, it has executed var xo; therefore, the output in the above Code is undefined.
The above is a detailed example of the JavaScript scope introduced by the small Editor. I hope it will be helpful to you. If you have any questions, please leave a message and the small editor will reply to you in time. Thank you very much for your support for the help House website!