我们有很多设置 JS 代码,用于定义面板、按钮等,这些代码将在许多其他 JS 文件中使用。
通常,我们会做这样的事情:
grid.js
var myGrid = .....
组合.js
var myCombo = .....
然后,在我们的应用程序代码中,我们:
应用程序.js
function blah() {
myGrid.someMethod()
}
someother.js
function foo() {
myCombo.someMethod();
myGrid.someMethod();
}
那么,我们应该使用var myGrid
或者更好地使用window.myGrid
有什么不同?
功能上的一个潜在的重要差异是window.myGrid
can be delete
d, and var myGrid
不能。
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 )
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)