


Authoritative Guide to JavaScript Study Notes Sharing Variable Scope_Javascript Skills
I don’t know how you understand the “declaration” and “definition” of variables in the language.
My understanding is as follows:
“Declaring” a variable means just declaring it, while “defining” a variable means declaring it. , and assigned a value.
For example:
var name;//Just a statement
var num = 11;//Declaration and assignment, that is, it is defined
var password = "yangjiang";//Declaration and assignment, that is, it is defined
The following are a few Point summary:
Scope of variables: global and local. (Note: If you try to read the value of an undeclared variable, JavaScript will generate an error)
First point: In the case of using the var keyword to modify variables, if a local variable or function parameter is declared The name is the same as the name of a global variable,
then the global variable is effectively hidden.
For example:
var scope1 = "global"; //var modification
function checksScope(){
var scope1 = "local";//var modification
document.write(scope1);
}checksScope();//local
Second point: If you try to give a variable declared without the var keyword, then the implicitly declared variable is always created as a global variable, even if
the variable is only used within a function body (It will only take effect if the function is run.) Note that function nesting is not supported.
For example:
scope2 = "globalAAAAAA";/ / No var modification is used (js will declare it as a global variable by default)
function checkScopeA(){
scope2 = "localAAAAA"; // No var modification is used (js will declare it as a global variable by default)
document.write("
" scope2);
myscope = "myLocalAAAAA";//No var modification is used (js will declare it as a global variable by default)
document.write ("," myscope);
}
checkScopeA();//localAAAAA, myLocalAAAA *A
document.write("
" scope2);//localAAAAA *B
document.write("
" myscope);//myLocalAAAAA *C
If you comment out the code at *A in the above example,
For example:
scope2 = "globalAAAAA";//No var modification is used (js will declare it as a global variable by default)
function checkScopeA(){
scope2 = "localAAAAA";//No var modification is used (js will declare it as a global variable by default)
document. write("
" scope2);
myscope = "myLocalAAAAA";//No var modification is used (js will declare it as a global variable by default)
document.write("," myscope );
}
//checkScopeA(); *A
document.write("
" scope2);//globalAAAAA *B
document.write("< br/>" myscope);//An error occurred *C
Because the function checkScopeA is not executed, the output at *B is globalAAAAA;
Because the function checkScopeA is not executed, the variable myscope is not Declaration, if you try to read an undeclared variable, an error will occur.
The third point:
In JavaScript, function definitions can be nested. Since each function has its own local scope, it is possible to have several nested levels of local scopes.
For example:
var scope3 = "global scope" ; //A global variable is defined
function checkScopeB(){
var scope3 = "local scope"; //A local variable is defined, overriding the global variable scope3
function nested(){
var scope3 = "nested scope"; //A local variable is defined inside the function of the function
document.write("
" scope3); //nested scope
}
nested();
}
checkScopeB();//nested scope
The fourth point:
In javascript, there is no block-level scope, it is declared in the function All variables, no matter where they are declared, are declared throughout the function.
In JavaScript, there is no block-level scope. All variables defined in a function, no matter where they are defined, are defined throughout the function.
For example:
function test(o){//According to the above description: the scopes of the three variables i, j, and k in this function are the same.
var i = 0; //Variable i is defined throughout the function
if(typeof o == "object"){
var j = 0; //Variable j is defined throughout the function Defined, not just in the if statement block
for(var k=0;k<10;k){//The variable k is defined in the entire function, not just in the if statement block
document.write("
The value of k is: " k);
}
document.write("
The value of k outside the for loop: " k); //K at this time is still defined, k=10
}
document.write("
value of j:" j); //Variable j has been declared, but it may It has not been initialized because the parameters passed into the function may not be objects, and the if statement block will not be executed
}
This function is called in two ways:
Method 1: Pass Input object
test({});//Output result: Comment in the above example
Method 2: Pass nothing
test();//Output result: value of j: undefined
What I don’t understand is why the output result in the second method is undefined. What I guessed at the time was: The value of j: 0
Later, this book said:
Since local variables are declared (or defined) in the entire function body, this means that in the entire function body The global
variable with the same name is hidden in . Although local variables are declared (or defined) throughout the function body, they will not be initialized before the var statement is executed.
In this case, the output result of the call in the second method above is easier to explain. Since the variable j is defined in the entire function, and since the parameters passed into the function are empty, the if statement in the function body will not be executed, thus making the value of j undefined. (This is my understanding based on the sentence in the book above)
The following example is a better explanation:
var sssss = "Global variable";
function f(){
document.write ("
" sssss);//Output: undefined instead of outputting "global variables"
var sssss = "local variables";
document.write("
" sssss);//Output: local variable
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The variable scope in PHP is divided into local (within the function), global (accessible within the program), and class scope (accessible within the class instance). The global keyword can declare local variables as global variables, and the static keyword can declare local variables as static variables, retaining their values between function calls.

In Go, the function life cycle includes definition, loading, linking, initialization, calling and returning; variable scope is divided into function level and block level. Variables within a function are visible internally, while variables within a block are only visible within the block.

Go language is an open source statically typed language. It has the characteristics of simplicity, efficiency and reliability, and is increasingly loved by developers. In the Go language, variables are the most basic form of data storage in programs. The scope and life cycle of variables are very important to the correctness and efficiency of the program. The scope of a variable refers to the visibility and accessibility of the variable, that is, where the variable can be accessed. In the Go language, the scope of variables is divided into global variables and local variables. Global variables are variables defined outside a function and can be used anywhere in the entire program

In Go, function scope limits variable visibility to the function where the variable is declared: Declare variables within a function: varnametype=value The scope is limited to the declared code block, and other functions or nested blocks cannot access these variables.

PHP5.6 variable scope: How to use the static keyword to define static variables In PHP, the scope of a variable determines the visibility and access scope of the variable. A static variable is a special type of variable that keeps its value unchanged between function calls. In PHP5.6 and above, you can use the static keyword to define static variables inside functions and class methods. The characteristics of static variables are: the scope of static variables is limited to the function or method in which it is declared. Static variables are used between function or method calls

PHP is a very popular web development language that allows developers to create dynamic web applications on the server side. In PHP, a variable is a basic data structure used to store values and data. This article will introduce how to use variables in PHP. Basic Syntax of Variables The syntax for declaring variables in PHP is very simple. Variable names begin with a dollar sign ($), followed by the variable name. Variable names can be a combination of letters, numbers, or underscores, but they must begin with a letter or an underscore. For example, the following code declares a name

Use Python's locals() function to get the variables of the current scope. In Python, locals() is a built-in function that can be used to get all variables in the current scope. This function returns a dictionary containing all variable names and corresponding values in the current scope. During the programming process, it is very useful to know the variables in the current scope, especially during the debugging stage, which can help us view the value and status of the variables. The locals() function provides exactly this function. Down

The variable scope of a Golang function refers to the visibility and life cycle of variables inside the function. According to the position and scope of variables in the function, variables can be divided into three types: local variables, parameter variables and return value variables. Detailed introduction: 1. Local variables are variables defined inside a function and can only be used inside the function. Their scope is limited to inside the function, including all code blocks and nested code blocks of the function; 2. Parameter variables , are the input parameters received by the function and can be used inside the function. Their scope is limited to the inside of the function, etc.
