Difference between variable declaration syntaxes in Javascript (including global variables)?

后端 未结 5 1883
梦毁少年i
梦毁少年i 2020-11-22 01:40

Is there any difference between declaring a variable:

var a=0; //1

...this way:

a=0; //2

...or:



        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 02:26

    Index.html
    
    
    
    /* external.js */
    
    console.info(varDeclaration == true); // could be .log, alert etc
    // returns false in IE8
    
    console.info(noVarDeclaration == true); // could be .log, alert etc
    // returns false in IE8
    
    console.info(window.hungOnWindow == true); // could be .log, alert etc
    // returns true in IE8
    
    console.info(document.hungOnDocument == true); // could be .log, alert etc
    // returns ??? in IE8 (untested!)  *I personally find this more clugy than hanging off window obj
    

    Is there a global object that all vars are hung off of by default? eg: 'globals.noVar declaration'

提交回复
热议问题