JavaScript variable scope analysis_javascript skills
/* Code 1 */
var scope = "global " ;
function checkScope() {
var scope = "local ";
function childCheck() {
var scope = "childLocal ";
document.write(scope);
}
function childUndefined() {
document.write(scope);
var scope;
}
function childOverride() {
scope = "childOverride ";
document .write(scope);
}
document.write(scope); //Output "local"
childCheck(); //Output "childLocal"
childUndefined(); //Output" undefined"
childOverride(); //Output "childOverride"
document.write(scope); //Output "childOverride"
}
checkScope(); //Output "local childLocal undefinedchildOverride childOverride "
document.write(scope); //Output "global "
Global scope and local scope
The scope of global (global) variables is global, in Javascript There are definitions everywhere; the variables declared inside the function are local variables, their scope is local, and they are only defined inside the function body. The following output should come as no surprise to readers.
/* Code 2 */
var scope = "global";
function checkScope() {
var scope = "local";
document.write(scope);
}
checkScope(); //Output "local"
document.write(scope); //Output "global"
You can use variables in the global variable scope without the var statement, but you must use the var statement when declaring local variables, otherwise it will Treated as a reference to a global variable. Look at the code below:
/* Code 3 */
var scope = "global";
function checkScope() {
scope = "local";
document.write(scope);
}
checkScope(); //output" local"
document.write(scope); //Output "local"
No block scope
Javascript does not have block-level scope, variables declared in the function are in the entire function They are all defined. The following code may be surprising to unfamiliar readers:
/* Code 4 */
var scope = "global";
function checkScope() {
document.write(scope); // Statement 4.1
var scope = "local"; / /Statement 4.2
document.write(scope);
}
checkScope(); //Output "undefinedlocal"
Due to statement 4.1 (var scope = "local"; ) The variables declared are valid within the entire checkScope function scope, so when statement 4.2 (document.write(scope); ) is executed, the scope refers to the local variable, and the local variable scope is not yet defined at this time, so "undefined" is output. . Therefore a good programming practice is to group all variable declarations at the beginning of the function.
After understanding the above content, readers should not be confused when looking at Code 1.
Attribute variables of objects
Attribute variables of objects are relatively easy to understand. Readers should not be confused if they look at the code below.
/* Code 5 */
var scope = "global ";
var obj = new Object();
obj.scope = "object ";
obj.checkScope = function () {
var scope = "loacl ";
document.write(scope); //Output "loacl"
document.write(this.scope); //Output "object"
document.write(window.scope); //Output "global"
}
obj.checkScope(); //Output "loacl object global"

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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.
