Detailed explanation of Javascript variable types and scope examples

伊谢尔伦
Release: 2017-07-18 09:59:29
Original
1157 people have browsed it

1. The scope of Javascript variables is divided according to method blocks (that is, divided by a pair of curly braces {} in function). Remember, it is a function block, and the for, while, and if blocks are not the standard for dividing scopes. You can look at the following examples:


Copy after login

2. Before Javascript is executed, it will perform a complete analysis of the declaration part of the entire script file (including local variables) to determine the scope of the real variables. How to understand it? Look at the following example:


Copy after login

Third, when the global variable has the same name as the local variable, the scope of the local variable will overwrite the scope of the global variable. When leaving the scope of the local variable After scope, it returns to the scope of the global variable. When the global variable encounters the local variable, how to use the global variable? Use window.globalVariableName.


Copy after login

Scope of variables

1. Depending on the scope of the variable, there are two types of variables: global variables and local variables. Variables defined in a function are local variables, and local variables are only valid within the function. If the local variable and the global variable use the same variable name, the local variable will overwrite the global variable

Copy after login

2. The JavaScript language does not have block scope

Copy after login

The above is the detailed content of Detailed explanation of Javascript variable types and scope examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!