In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.