In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "JavaScript scope instance code analysis". In daily operation, I believe that many people have doubts about JavaScript scope instance code analysis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "JavaScript scope case code analysis". Next, please follow the editor to study!
1. Local JavaScript variable
A variable is declared inside the JavaScript function and becomes a local variable of the function.
Local variables have local scope: they can only be accessed in functions.
JS://code here can not use carName reFunction (); document.getElementById ("demo") [xss_clean] = "Type of carName is" + typeof carName; function reFunction () {var carName = "Volvo";}
Because local variables are only recognized in their functions, variables with the same name can be used in different functions.
Create a local variable when the function starts and delete it when the function is complete.
2. Global JavaScript variable
A variable declared outside the function becomes a global variable.
A global variable has a global scope: it can be accessed by all scripts and functions on a web page.
Var carName = "Volvo"; / / the global variable myFunction () can be accessed from any script or function; function myFunction () {document.getElementById ("demo") [xss_clean] = "I can display" + carName;}
Automatic global
If you assign a value to an undeclared variable, it automatically becomes a global variable.
This code example declares a global variable carName, even if the value assigned is inside the function.
MyFunction (); / / code here can use carName function myFunction () {carName = "Volvo";}
Do not create a global variable unless you need it very much, the "Strict Mode" automatic global variable will fail in strict mode.
Global variables in HTML
In JavaScript, the global scope is the complete JavaScript environment.
In HTML, the global scope is the window object. All global variables belong to the window object.
Project
In HTML, all global variables become window window variables.
Var carName = "Volvo"; / / code here can use window.carName document.getElementById ("demo") [xss_clean] = "I can display" + window.carName
Global variables (or functions) can override window variables (or functions). Any function, including window objects, can override global variables and functions.
Fourth, JavaScript code block scope
The table shows the difference between var,let and const.
Variables declared with the var keyword cannot have block scope, and {} variables declared inside the block can be accessed from outside the block:
{var num = 50;} / / num can be used here
Variables declared with the let keyword can have "block scope".
{} variables declared inside the block cannot be accessed from outside the block:
{let num = 50;} / / num cannot be used here
The declaration variable with const is similar to let] in terms of block scope.
{const num = 50;} / / num cannot be used here
The value of a constant cannot be changed by reallocation, nor can it be redeclared.
At this point, the study on "JavaScript scope instance code analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.