JavaScript YUI3使用全局变量吗?

我不知道如何从YUI3中更新全局变量.

考虑以下代码:

window.myVariable = 'data-one';
var yuiWrap = YUI().use('node',function(Y) {
  console.log(window.myVariable); // 'data-one'
  window.myVariable = 'data-two';
  console.log(window.myVariable); // 'data-two'
});
console.log(window.myVariable); // 'data-one'

有人可以向我解释吗?这给我带来很多麻烦.为什么从YUI3块中可以访问window.myVariable但不能正确地对其进行更新?

我认为这可能与Closures有关,但我不明白为什么闭包应应用于全局“窗口”对象.

救命?

解决方法:

回调不会立即触发,但会发生某些情况:

Attaches one or more modules to the YUI instance. When this is executed, the requirements are analyzed, and one of several things can happen:

  • All requirements are available on the page — The modules are attached to the instance. If supplied, the use callback is executed synchronously.
  • Modules are missing, the Get utility is not available OR the ‘bootstrap’ config is false — A warning is issued about the missing modules and all available modules are attached.
  • Modules are missing, the Loader is not available but the Get utility is and boostrap is not false — The loader is bootstrapped before doing the following….
  • Modules are missing and the Loader is available — The loader expands the dependency tree and fetches missing modules. When the loader is finshed the callback supplied to use is executed asynchronously.
上一篇:javascript-并非所有项目都可以用Scriptaculous,Sortables,滚动和溢出进行排序


下一篇:javascript-在YUI中获取Clicked项目的索引