zoukankan      html  css  js  c++  java
  • Javascript 中的变量

    var a;
    console.log("The value of a is " + a); // The value of a is undefined
    
    console.log("The value of b is " + b); // The value of b is undefined
    var b = 3;
    
    console.log("The value of c is " + c); // Uncaught ReferenceError: c is not defined
    
    let x;
    console.log("The value of x is " + x); // The value of x is undefined
    
    console.log("The value of y is " + y); //Uncaught ReferenceError
    let y;

    变量提升,使用var 时,例子中的b 会被提升,但值仍未undefined 。 使用let 可以避免提示,打印 y 时将报错。

    /**
     * Example 1
     */
    console.log(x === undefined); // true
    var x = 3;
    
    /**
     * Example 2
     */
    // will return a value of undefined
    var myvar = "my value";
     
    (function() {
      console.log(myvar); // undefined
      var myvar = "local value";
    })();

    注意上面的自调用函数中,myvar 是指local 域中,它覆盖了global域中的myvar =“my value"; 所以myvar 被提示,值为undefined。

    /* Function declaration */
    
    foo(); // "bar"
    
    function foo() {
      console.log("bar");
    }
    
    
    /* Function expression */
    
    baz(); // TypeError: baz is not a function
    
    var baz = function() {
      console.log("bar2");
    };

    函数声明将会被提升,而函数表达式将不会提升,所以baz()将报错

  • 相关阅读:
    Linux Shell 01 脚本与变量
    Linux下shell颜色配置
    Linux下Redis安装及配置
    Linux Shell 03 条件测试
    OSX下VirtualBox安装CentOS
    Log4j配置与使用
    Linux 环境变量的配置
    OS X下安装Redis及配置开机启动
    圈复杂度
    (转)Qt Model/View 学习笔记 (一)——Qt Model/View模式简介
  • 原文地址:https://www.cnblogs.com/neverleave/p/6047453.html
Copyright © 2011-2022 走看看