JavaScript YUI3使用全局变量? [英] JavaScript YUI3 using global variables?

查看:116
本文介绍了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'

任何人都可以向我解释这个问题吗?这让我很麻烦。为什么可以访问window.myVariable但是没有从YUI3块中正确更新?



我认为它可能与 Closures ,但我不明白为什么Closures应该应用于全局窗口对象。

$ b $




将一个或多个模块附加到YUI实例。当执行时,分析需求,可以发生以下几种情况之一:




  • 页面上提供了所有要求 - 模块附加到实例。如果提供,则会同步执行use回调。

  • 模块丢失,Get实用程序不可用或'bootstrap'配置为false - 发出有关缺少模块的警告所有可用的模块都已附加。

  • 模块缺失,Loader不可用,但Get实用程序是,boostrap不为false - 在执行以下操作之前,引导加载程序....

  • 模块丢失且装载器可用 - 装载器扩展依赖关系树并提取缺失的模块。 当加载器耗尽时,提供的回调将异步执行。



I can't work out how to update a global variable from within YUI3.

Consider the following code:

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'

Can anyone explain this to me? It's causing me a lot of trouble. Why can window.myVariable be accessed but not properly updated from within a YUI3 block?

I think it might have something to do with Closures but I don't understand why Closures should apply to the global "window" object.

Help?

解决方案

The callback is not fired immediately but after something happened:

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 YUI3使用全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆