zoukankan      html  css  js  c++  java
  • js的函数作用域

    1.js作用域

    //在函数内部声明的变量,如果不加var,则自动变成window的成员
    //预处理:扫描代码,看到var或者函数就生效。
    /*function f(){
    a = 8;
    //var a = 8;
    }
    f();
    alert(a);
    alert(window.a);
    */

    //*********练习1*********
    /*alert(a);
    alert(b);
    if(8>5){
    var a= 10;
    }else{
    var b= 100;
    }
    alert(a);
    alert(b);*/

    //*********练习2*********
    /*alert(a);//报错
    alert(b);//undefined
    if(8>5){
    a= 10;
    }else{
    var b= 100;
    }
    alert(a);//10
    alert(b);//undefined*/

    //*********练习3*********
    /*function f(){
    var a = 5;
    function g(){
    alert(a);
    }
    g();
    }
    f();*/
    //*********练习4*********
    /*function f(){
    a = 5;
    function g(){
    alert(a);//5
    }
    g();
    }
    f();*/

    //*********练习4*********
    //闭包
    //在js预处理阶段,用到的a=5,被捕获,
    /*function f(){
    a = 5;
    return function g(){
    alert(a);//5
    }
    }
    //定义一个result来接受数据
    var result = f();
    result();*/

    //*********练习5*********
    function f(a){
    alert(a);
    function a(){}
    function a(){alert("第二个函数");}
    function a(){alert("最后一个函数");}
    }
    f(5);

  • 相关阅读:
    浅析跨域请求
    python虚拟环境--virtualenv
    centos7下使用yum安装pip
    centos下python安装与虚拟环境配置
    ES6基础语法
    CCI_chapter 19 Moderate
    CCI_chapter 16 Low level
    CCI_chapter 13C++
    CCI_chapter 8 Recurision
    LeetCode_Generate Parentheses
  • 原文地址:https://www.cnblogs.com/goldlong/p/7904074.html
Copyright © 2011-2022 走看看