本文介绍了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应该应用于全局窗口对象。
$ b $
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:
这篇关于JavaScript YUI3使用全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!