Home  >  Article  >  Web Front-end  >  Detailed explanation of Javascript variable types and scope examples

Detailed explanation of Javascript variable types and scope examples

伊谢尔伦
伊谢尔伦Original
2017-07-18 09:59:291244browse

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:


<script>
function test2(){
  alert ("before for scope:"+i);  // i未赋值(并不是未声明!使用未声明的变量或函数全抛出致命错误而中断脚本执行)
                          // 此时i的值是underfined
  for(var i=0;i<3;i++){
    alert("in for scope:"+i); // i的值是 0、1、2, 当i为3时跳出循环
  }
  alert("after for scope:"+i); // i的值是3,注意,此时已经在for scope以外,但i的值仍然保留为3  
  while(true){
    var j = 1;
    break;
  }
  alert(j);  // j的值是1,注意,此时已经在while scope以外,但j的值仍然保留为1
  if(true){
    var k = 1;
  }
  alert(k); //k的值是1,注意,此时已经在if scope以外,但k的值仍然保留为1
}
test2();
//若在此时(function scope之外)再输出只存在于test2 这个function scope里的 i、j、k变量会发生神马效果呢?
alert(i); //error! 没错,是error,原因是变量i未声明(并不是未赋值,区分test2函数的第一行输出),导致脚本错误,程序到此结束!
alert("这行打印还会输出吗?"); //未执行
alert(j); //未执行
alert(k); //未执行
</script>

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:


<script>
  var a =1;
  function test(){
    alert(a); //a为undefined! 这个a并不是全局变量,这是因为在function scope里已经声明了(函数体倒数第4行)一个重名的局部变量,
           //所以全局变量a被覆盖了,这说明了Javascript在执行前会对整个脚本文件的定义部分做完整分析,所以在函数test()执行前,
           //函数体中的变量a就被指向内部的局部变量.而不是指向外部的全局变量. 但这时a只有声明,还没赋值,所以输出undefined。
    a=4    
    alert(a); //a为4,没悬念了吧? 这里的a还是局部变量哦!
    var a;   //局部变量a在这行声明
    alert(a); //a还是为4,这是因为之前已把4赋给a了
  }
  test();
  alert(a); //a为1,这里并不在function scope内,a的值为全局变量的值
</script>

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.


<script>
  var a =1;
  function test(){  
    alert(window.a); //a为1,这里的a是全局变量哦!
    var a=2;   //局部变量a在这行定义
    alert(a); //a为2,这里的a是局部变量哦!
  }
  test();
  alert(a); //a为1,这里并不在function scope内,a的值为全局变量的值
</script>

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

<script>   
    //定义全局变量test   
    var test = "全局变量";    
    function checkscope(  )   
    {   
        var test = "局部变量";   
        alert(test);   
    }   
    checkscope(  );    
//在函数里定义的变量为局部变量,局部变量只在函数内有效。   
//如果局部变量和全局变量用相同的变量名,则局部变量将覆盖全局变量   
</script>

2. The JavaScript language does not have block scope

<script>   
    function test(o)       
    {   
        //定义变量i,变量i的作用范围是整个函数   
            var i = 0;   
        if (typeof o == "object")    
        {   
            //定义变量j,变量j的作用范围是整个函数内,而不是if块内。   
                var j = 5;   
                for(var k = 0; k < 10; k++)   
                {   
                //k的作用范围是整个函数内,而不是循环体内   
                    document.write(k);   
                }   
        }      
        //即使出了循环体,k的值依然存在   
        alert(k + "\n" + j);    
    }   
    test(document);   
</script>

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!

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