Should I use window.variable or var?

前端 未结 7 1650
生来不讨喜
生来不讨喜 2020-12-02 12:25

We have a lot of setup JS code that defines panels, buttons, etc that will be used in many other JS files.

Typically, we do something like:

grid.js

相关标签:
7条回答
  • 2020-12-02 12:55

    A potentially important difference in functionality is that window.myGrid can be deleted, and var myGrid can not.

    var test1 = 'value';
    window.test2 = 'value';
    
    
    console.log( delete window.test1 ); // false ( was not deleted )
    console.log( delete window.test2 ); // true  ( was deleted )
    
    
    console.log( test1 );  // 'value'         ( still accessible )
    console.log( test2 );  // ReferenceError  ( no longer exists )
    
    0 讨论(0)
提交回复
热议问题