In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the scope in javascript? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
In javascript, scope is the accessible scope of variables (objects, functions) and the effective scope for variables to be read and written in script code; scope can control the visibility and life cycle of variables.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Almost all languages have the concept of scope. To put it simply, scope is the accessible scope of variables, that is, scope controls the visibility and life cycle of variables.
In JavaScript, objects and functions are also variables.
Before ECMAScript6, the scope of variables is mainly divided into global scope and local scope (also known as function scope); after ECMAScript6, the scope of variables is mainly divided into global scope, local scope and block scope.
The variables in the corresponding scope are called global variables, local variables and block-level variables.
Global variables are declared outside all functions
A local variable is a variable declared in the body of a function or a named parameter of a function
Block-level variables are variables declared in a block and are valid only in a block.
The scope of a variable is closely related to the way it is declared. Variables declared with var have global and functional scopes and no block-level scope; variables declared with let and const have global scope, local scope, and block-level scope.
Var v1 = "JavaScript"; / / global variable let v2 = "JScript"; / / global variable let v3 = "Script"; / / global variable scopeTest (); / / call function function scopeTest () {var lv = "aaa" / / local variable var v1 = "bbb"; / / local variable let v2 = "ccc"; / / local variable if (true) {let lv = "123" / / Block-level variable console.log ("lv output within the block =" + lv); / / 123} console.log ("lv output from the function body =" + lv) / / aaa console.log ("function body output v1 =" + v1); / / bbb console.log ("function body output v2 =" + v2); / / ccc console.log ("function body output v3 =" + v3) / / Script / / v4 is a global variable, assigned later, so the value is undefined console.log ("function body output v4 =" + v4);} var v4 = "VBScript" / / global variable console.log ("function external output lv =" + lv); / / ① reports ReferenceError error console.log ("function external output v1 =" + v1); / / JavaScript console.log ("function external output v2 =" + v2) / / JScript console.log ("function external output v3 =" + v3); / / Script console.log ("function external output v3 =" + v4); / / VBScript
The above script code declares four global variables, three local variables, and one block-level variable. Outside the scopeTest function, variables v1, v2, v3 and v4 are global variables; in the scopeTest function body, lv and v2 are global variables; in the if judgment block, lv is a block-level variable.
We can see that the local variables v1 and v2 have the same name as the global variables v1 and v2. In the body of the scopeTest function, the local variables v1 and v2 are valid, so the output results of these two variables in the body of the function are "bbb" and "ccc" respectively; outside the function, the global variables v1 and v2 are valid, so outside the function, the output results of these two variables are "JavaScript" and "JScript" respectively.
In addition, the block-level variable lv and the local variable lv have the same name. In the if judgment block, the block-level variable lv is valid, so the output result in the block is "123", while outside the block, the local variable lv is valid, and the output result of the lv variable is "aaa".
In addition, the global variables v3 and v4 are not covered in the function body, so the output of v3 is the value of the global variable, so the output of v3 inside and outside the function is "Script", while the v4 variable assignment is after the function call, so the v4 output in the function body is "undefined", while the output outside the function is after the declaration, so the result is "VBScript". Lv is a local variable, so accessing the function outside the body will report a "ReferenceError" error.
After the above code runs in the Chrome browser, open the browser's console, and you can see the output shown in the following figure.
The reason why 28 lines of code reported an error:
The lv variable is local and has no effect after leaving the function. Comment this line of code and then run it, and then open the browser console to see:
What are the characteristics of JavaScript? 1. Js belongs to an explanatory scripting language. 2. With the support of most browsers, js can run on multiple platforms and has cross-platform characteristics. 3. Js is a weakly typed scripting language that does not make strict requirements on the data types used. It is able to type conversion, simple and easy to use. 4. Js language is highly secure, and it can only browse information or interact dynamically through the browser, so as to effectively prevent the loss of data. 5, object-based scripting language, js can not only create objects, but also use existing objects.
This is the answer to the question about what the scope in javascript refers to. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.