• JS 作用域 实例分析


    /*  此模块测试变量作用域 star */
    var a ;
    console.log(a); //undefined
    function testScope0(){
        a = 10;
        console.log(a); //10
    }
    testScope0();
    /*  此模块测试变量作用域 end */
    
    /*  此模块测试变量作用域 star */
     var a = 100;
     console.log(a); //100
    
    function testScope1(){
        a = 10;
        console.log(a); //10
    }
    testScope1();
    
    function testScope2(){
        var a = 10;
        console.log(a); //10
    }
    testScope2();
    
    function testScope3(){
        var a = 10;
        console.log(a); //10
        console.log(this.a); //100
    }
    testScope3();
    
    function testScope4(){
        a = 10;
        console.log(a); //10
        console.log(this.a); //100
        var a =1000;
        console.log(a);//1000
    }
    testScope4();
    
    /*  此模块测试变量作用域 end */
    
    /*  此模块测试变量作用域 star */
    var a = b = null;
    var c = 1;
    (function a(v){
        console.log(c); //1
        console.log(v); //1
     return a = v;
    })(c)
     console.log(a); //null
    
    var a = b = null;
    var c = 1;
    function otherScope(c){
        console.log(c); //1
        a = c;
        console.log(a); //1
     return a;
    }
    otherScope(c);
    console.log(a); //1
    
    /*  此模块测试变量作用域 end */
    
    /*  此模块测试变量作用域 star */
    var name = "window";
    var obj = {
        name:"the obj",
        getName:function(){
            return this.name
        }
    }
    console.log(obj.getName()); //the obj
    /*  此模块测试变量作用域 end */
    
    /*  此模块测试变量作用域 star */
    var name = "window";
    var obj = {
        name:"the obj",
        getName:function(){
                    return function(){
                        return this.name;
                    }
                }
        }
    console.log(obj.getName()()); //window
    /*  此模块测试变量作用域 end */
    
    /*  此模块测试变量作用域 star */
    var a;
    if(a in window){
     console.log("yes"); //yes
    } 
    
    if(!(a in window)){
        var a = 1;
    }
    console.log(a); //undefined
    /*  此模块测试变量作用域 end */
  • 相关阅读:
    LoadScript
    Mac终端Terminal调用Sublime Text
    jquery ajax 提交 FormData
    ps切图设置
    bootstrap-select搜索框输入中文
    Homebrew安装
    清空file input框
    javascript 随机数区间
    Canvas与Image互转
    html空格小结
  • 原文地址:https://www.cnblogs.com/xmyun/p/5923749.html
Copyright © 2020-2023  润新知