kla*_*dos 1 javascript windows-8
我想在.js文件中使用全局变量.例如,
当我在"a.js + a.html"中读取文件内容并保存到某个变量"fileContent"时
那么我也想在"b.js + b.html"中使用'fileContent'.
(另外,当我更改"b.js + b.html"中的'fileContent'时,应该影响"a.js + a.html"中的'fileContent')
我该怎么办?
谢谢.
鉴于Windows 8应用程序的体系结构是单页面模型,您不在浏览器中导航,而只是加载HTML片段并将其插入当前文档,这非常简单.但是,我建议使用一些打字,而不仅仅是"裸"的全局变量.
文件A(globals.js):
WinJS.Namespace.define("MyGlobals", {
variableA: null,
});
Run Code Online (Sandbox Code Playgroud)
在WinJS包含后,将其包含在default.html的顶部.
档案B(b.js):
// your other code
...
MyGlobals.variableA = getValueFromSomewhere();
Run Code Online (Sandbox Code Playgroud)
文件C(b.html或c.js):
// your other code
...
printSomethingAwesomeFromData(MyGlobals.variableA);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4517 次 |
最近记录: |