Home > Web Front-end > JS Tutorial > body text

Javascript variable scope

高洛峰
Release: 2016-11-30 16:50:21
Original
951 people have browsed it

论 Analysis: 是 Whether it is a strong type of language C#, C ++, Java and other languages, or weak types of language such as JavaScript, all variables can be abstracted into two types, namely local variables and global variables.

​​​​​​ Global variables: The entire scope is visible. Local variables: Local visible, exit, is destroyed by GC and recycling space.

 Code analysis:

//局部变量
function PartVary() {
    var n = 10;  //表示局部变量,外部不可访问
}
 
PartVary();
alert(n);//error
<br>
//全局变量
function AllVary() {
     n = 10  //表示全部变量,外部可访问
}
PartVary();
alert(n);//10
<br>
var n = 10;
function AllVary() {
    alert(n);
}
 
AllVary();//10
Copy after login

 Summary:

    ​​ Regarding the visibility of variables, those with low scope can access those with high scope, however, those with high scope cannot access those with high scope. Abstract: parent shields child

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template