As Fermin said, a variable in the global scope should be accessible to all scripts loaded after it is declared.
(正如Fermin所说,全局范围中的变量应该可以被声明后加载的所有脚本访问。)
You could also use a property of window
or (in the global scope) this
to get the same effect.(您也可以使用的属性window
或(在全球范围内), this
得到同样的效果。)
// first.js
var colorCodes = {
back : "#fff",
front : "#888",
side : "#369"
};
... in another file ...
(...在另一个文件中......)
// second.js
alert (colorCodes.back); // alerts `#fff`
... in your html file ...
(...在你的html文件中......)
<script type="text/javascript" src="first.js"></script>
<script type="text/javascript" src="second.js"></script>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…