zoukankan      html  css  js  c++  java
  • Variable hoisting Global variables Constants

     

    Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense "hoisted" or lifted to the top of the function or statement. However, variables that aren't initialized yet will return a value of undefined.

    /**
     * Example 1
     */
    console.log(x === undefined); // logs "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";
    })();
    

      

     

    The above examples will be interpreted the same as:

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

      

    Because of hoisting, all var statements in a function should be placed as near to the top of the function as possible. This best practice increases the clarity of the code.

    Global variables

    Global variables are in fact properties of the global object. In web pages the global object iswindow, so you can set and access global variables using the window.variable syntax.

    Consequently, you can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called phoneNumber is declared in a document, you can refer to this variable from an iframe as parent.phoneNumber.

    Constants

    You can create a read-only, named constant with the const keyword. The syntax of a constant identifier is the same as for a variable identifier: it must start with a letter, underscore or dollar sign and can contain alphabetic, numeric, or underscore characters.

    const prefix = '212';
    

      

     

    A constant cannot change value through assignment or be re-declared while the script is running. It has to be initialized to a value.

    The scope rules for constants are the same as those for let block scope variables. If theconst keyword is omitted, the identifier is assumed to represent a variable.

    You cannot declare a constant with the same name as a function or variable in the same scope. For example:

    // THIS WILL CAUSE AN ERROR
    function f() {};
    const f = 5;
    
    // THIS WILL CAUSE AN ERROR ALSO
    function f() {
      const g = 5;
      var g;
    
      //statements
    }
    

      

  • 相关阅读:
    冲销收货
    收货MIGO
    删除PO
    创建po
    File的功能--> 获取功能-->所有的根目录 | 创建文件功能,但是如果文件已经存在-->不再创建(新手)
    在d盘创建文件夹,里面有aaa.txt/bbb.txt/ccc.txt,然后遍历出aaa文件夹下的文件(新手)
    创建一个唱歌类集合,创建一个电影类集合,让其归类打印(新手)
    抛出异常(新手)
    Map-->HashMap练习(新手)
    JAVA-迭代器增强型for循环。(新手)
  • 原文地址:https://www.cnblogs.com/hephec/p/4601235.html
Copyright © 2011-2022 走看看