Is using 'var' to declare variables optional?

This question already has an answer here:

  • What is the purpose of the var keyword and when should I use it (or omit it)? 18 answers

  • They mean different things. If you use var the variable is declared within the scope you are in (eg of the function). If you don't use var , the variable bubbles up through the layers of scope until it encounters a variable by the given name or the global object (window, if you are doing it in the browser), where it then attaches. It is then very similar to a global variable. However, it can still be deleted with delete (most likely by someone else's code who also failed to use var ). If you use var in the global scope, the variable is truly global and cannot be deleted.

    This is, in my opinion, one of the most dangerous issues with javascript, and should be deprecated, or at least raise warnings over warnings. The reason is, it's easy to forget var and have by accident a common variable name bound to the global object. This produces weird and difficult to debug behavior.


    This is one of the tricky parts of Javascript, but also one of its core features. A variable declared with var "begins its life" right where you declare it. If you leave out the var , it's like you're talking about a variable that you have used before.

    var foo = 'first time use';
    foo = 'second time use';
    

    With regards to scope, it is not true that variables automatically become global. Rather, Javascript will traverse up the scope chain to see if you have used the variable before. If it finds an instance of a variable of the same name used before, it'll use that and whatever scope it was declared in. If it doesn't encounter the variable anywhere it'll eventually hit the global object ( window in a browser) and will attach the variable to it.

    var foo = "I'm global";
    var bar = "So am I";
    
    function () {
        var foo = "I'm local, the previous 'foo' didn't notice a thing";
        var baz = "I'm local, too";
    
        function () {
            var foo = "I'm even more local, all three 'foos' have different values";
            baz = "I just changed 'baz' one scope higher, but it's still not global";
            bar = "I just changed the global 'bar' variable";
            xyz = "I just created a new global variable";
        }
    }
    

    This behavior is really powerful when used with nested functions and callbacks. Learning about what functions are and how scope works is the most important thing in Javascript.


    Nope, they are not equivalent.

    With myObj = 1; you are using a global variable.

    The latter declaration create a variable local to the scope you are using.

    Try the following code to understand the differences:

    external = 5;
    function firsttry() {
      var external = 6;
      alert("first Try: " + external);
    }
    
    function secondtry() {
      external = 7;
      alert("second Try: " + external);
    }
    
    alert(external); // Prints 5
    firsttry(); // Prints 6
    alert(external); // Prints 5
    secondtry(); // Prints 7
    alert(external); // Prints 7
    

    The second function alters the value of the global variable "external", but the first function doesn't.

    链接地址: http://www.djcxy.com/p/96488.html

    上一篇: 在javascript程序中何处使用“var”

    下一篇: 是否使用'var'声明变量是可选的?