<script src="http://domain.com/source1.js"></script>
<script src="http://domain.com/source2.js"></script>
source1.js
var PICTURE_PATH = "";
var PICTURE_ROOT = base_url+"YOUTAILOR_files/";
var PROGRAM = parseInt("1");
source2.js
if(PROGRAM==3 || PROGRAM==4 || PROGRAM==5)
{
}
我无法访问source2.js中程序的值。
当在函数外声明var source1Var时,实际上是在window对象上创建变量。因此,您应该只能够从source2.js访问它。只要确保脚本标记中的source1.js在source2.js之前...
但是,如果您在函数中声明它,则该函数将私有于该函数。如果确实需要,可以在窗口上显式设置它。
在source1.js中
function foo() {
window.source1Var = 3;
}
在source2.js中
function bar() {
console.log(source1Var); // this will search window if it cannot find in the local scope.
}
您要解决的问题是什么?通常最好使用某种形式的依赖项注入,事件侦听器,构建器模式等,而不是使用全局变量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句