Home >Web Front-end >JS Tutorial >Javascript variable scope

Javascript variable scope

高洛峰
高洛峰Original
2016-11-30 16:50:21955browse

论 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

 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

Statement:
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