zoukankan      html  css  js  c++  java
  • [label][JavaScript][The Defined Guide of JavaScript] 变量的作用域

    变量的作用域

        一个变量的作用域(scope)是程序中定义这个变量的区域。

        全局(global)变量的作用域(scope)是全局性的,即在JavaScript代码中,它处处都有定义。
        而在函数之内声明的变量,就只在函数体内部有定义,它们是局部(local)变量,作用域是局部性的。
        函数的参数也是局部变量,它们只在函数体内部有定义。
        在函数体内部,局部变量的优先级比同名的局部变量高。
        
        如果给一个局部变量或函数的参数声明的名字与某个全局变量的名字相同,那么就有效地隐藏了这个全局变量。
      

     例如,下面的代码将输出单词"local scope":
            var scope = 'global scope';     // Declare global variable
                function checkScope() {
                    var scope = 'local scope';     // Declare a local variable with the same name
                    document.write(scope);        // Use the local variable. not the global one
                }
                
                checkScope(); // Prints "local"
        注意:在全局作用域中编写代码是可以不使用var语句,但是在声明局部变量时,一定要使用var语句。
        
        没有块级作用域
        JavaScript没有块级作用域,函数中声明的所有变量,无论是在哪里声明的,在整个函数中它们都是有
        定义的。
        
        下面代码中,变量i、j和k的作用域是相同的,它们三个在整个函数体中有定义。
            funtion test(o) {
                var i = 0;         // i is defined throughout function
                if(typeof o == "object") { // if block
                    var j = 0;    // j is defined everywhere, not just block
                    for(var k = 0; k < 10; k++) { // k is everywhere, not just loop
                        document.write(k);    // k is still defined: prints 10
                    }
                }
                document.write(j);    // j is defined, but may not be initialized
            }
            
            // Example-2
            
            var scope = 'global';    
            function f() {
                alert(scope);    // Displayed "undefine", not "global"
                var scope = "local";    // Variable initialized here, but defined everywhere
                alert(scope);    // Displays "local"
            }
            //由于这个作用域规则的限制,第一个alert输出的并不是"global"。
            //局部变量在整个函数体内都是有定义的,这就意味着在整个函数体中都隐藏了同名的全局变量。
            //虽然局部变量在整个函数体中都是有定义的,但是在执行var语句之前,它是不会被初始化的。
            因此上面的例子中,函数f和下面的函数等价:
            
            function f() {
                var scope;    // 局部变量在函数开头声明
                alert(scope);    // 此处该变量有定义,但值仍为"undefined"
                scope = 'local';     // 现在初始化该变量,并给它赋值
                alert(scope);    //此处该变量具有值
            }
            //这个例子说明了为什么将所有的变量声明集中起来放置在函数的开头是一个好的编程习惯。

    以下内容来自:

            http://www.cnblogs.com/wangfupeng1988/p/3986420.html

            http://www.cnblogs.com/wangfupeng1988/p/ 

    Code 1:

    console.log(a);   // variable a is undeclared and this will occure error reporting

    Code 2:

    console.log(a);   // undefined , declare a variable in JavaScript and not assign a value. Then the variable's default value is undefined.

    var a;

    Code 3:

    console.log(a);  // undefied

    var a = 10;

    Code 4:

    console.log(this);  // 这里输出将会是window对象,window对象是一个全局的对象

    Code 5:

    console.log(f1);  // function f1(){ }

     function f1() { }; // 声明函数

    Code 6:

     console.log(f2) ;  // undefined

    var f2 = function f2() { }; // 函数表达式,这种方式产生的效果和变量的声明并赋值一样

    注意: 学过C、Java都知道,在使用一个变量之前必须要先声明数据类型,如: int a; 

    同时,在声明变量的同时也可以初始化赋值,如: int a = 1001;

    但是在程序的执行过程之中, 是先进行类型的声明的预处理,然后就是执行赋值,"="是赋值操作符。

     

  • 相关阅读:
    序一
    Python3 网络爬虫开发实战
    爬虫笔记
    celery
    用yield实现协程 和asyncio模块
    Django学习之完成数据库主从复制、读写分离和一主多从情况下的使用办法
    Django学习之缓存和信号
    Django学习之Django-debug-toobar
    Python面向对象
    Python中 if __name__ == "__main__" 的理解
  • 原文地址:https://www.cnblogs.com/shuman/p/4015880.html
Copyright © 2011-2022 走看看