编程语言
首页 > 编程语言> > JavaScript YUI3使用全局变量吗?

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.

标签:closures,yui3,javascript,yui
来源: https://codeday.me/bug/20191102/1992810.html