An example Analysis of the scope of variables in JavaScript
This article mainly explains "the scope instance analysis of variables in JavaScript". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "the scope of variables in JavaScript instance analysis" bar!
I. Classification of variables
There are two types of variables in JavaScript:
Global variable
Local variable
Scope of variables 1. Scope of local variables
Local variable: a variable defined inside a function is called a local variable. Its scope is inside the function and cannot be accessed outside the function. Look at the following example:
Local variable scope / / define function fn function fn () {var axi5governance / define local variable [xss_clean] (a);}; / call function fn fn (); / / define function fn2 function fn2 () {document (a);} / / call the function fn2 fn2 ()
Results:
2. Global variables
Global variables: variables defined outside the function are called global variables, and their scope is the entire block of JavaScript code. Look at the following example:
Global variable scope var number; / / Global variable function fn () {number=5; [xss_clean] ("value of number is" + number+ ");}; function fn2 () {+ + number; [xss_clean] (" value of number is "+ number+");} fn (); fn2 ()
Results:
Note:
A. if a local variable with the same name as a global variable is defined in the function, then the proximity principle is used inside the function: that is, the local variable works inside the function.
Look at the following example:
Global variable scope var number; / / global variable / * function fn () {number=5; [xss_clean] ("value of number is" + number+ ");}; function fn2 () {+ + number; [xss_clean] (" value of number is: "+ number+") The nearest principle function fn () {var number= "I am a local variable"; [xss_clean] ("the value of number is" + number+ ");}; function fn2 () {number=" I am a global variable "[xss_clean] (" the value of number is "+ number+");} fn () Fn2 ()
Results:
B, global variable window
If you do not use var when defining variables, the default is global variables, both outside and inside the function. Look at the following example:
Global variable scope var number; / / global variable / * function fn () {number=5; [xss_clean] ("value of number is" + number+ ");}; function fn2 () {+ + number; [xss_clean] (" value of number is: "+ number+") } * / / proximity principle / * function fn () {var number= "I am a local variable"; [xss_clean] ("the value of number is" + number+ ");}; function fn2 () {number=" I am a global variable "[xss_clean] (" the value of number is: "+ number+") } fn (); fn2 (); * / / windows / / equivalent to window.an astat12; function fn () {/ / equivalent to window.b b = "I am a window object and a global variable";}; fn (); [xss_clean] ("a =" + a + "") [xss_clean] ("b =" + b + "")
Results:
C. Global variables should be avoided as far as possible to avoid conflicts between team development variables. At this point, I believe you have a deeper understanding of the "scope instance analysis of variables in JavaScript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!