Home Web Front-end JS Tutorial JavaScript variable scope analysis_javascript skills

JavaScript variable scope analysis_javascript skills

May 16, 2016 pm 06:05 PM
variable scope

Copy code The code is as follows:

/* 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.
Copy code The code is as follows:

/* 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:
Copy code The code is as follows:

/* 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:
Copy code The code is as follows:

/* 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.
Copy code The code is as follows:

/* 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"
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How is the variable scope of a PHP function determined? How is the variable scope of a PHP function determined? Apr 16, 2024 pm 04:51 PM

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-depth understanding of Golang function life cycle and variable scope In-depth understanding of Golang function life cycle and variable scope Apr 19, 2024 am 11:42 AM

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.

Variable scope and life cycle in Go language Variable scope and life cycle in Go language Jun 01, 2023 pm 12:31 PM

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

How to define variable scope in Golang function? How to define variable scope in Golang function? Apr 11, 2024 pm 12:27 PM

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.

PHP 5.6 variable scope: How to define static variables using static keyword PHP 5.6 variable scope: How to define static variables using static keyword Jul 30, 2023 pm 11:02 PM

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

How to use variables in PHP How to use variables in PHP May 20, 2023 pm 02:33 PM

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 variables in the current scope Use Python's locals() function to get variables in the current scope Aug 21, 2023 pm 09:52 PM

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

What is the variable scope of Golang function What is the variable scope of Golang function Dec 22, 2023 pm 02:39 PM

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.

See all articles