Javascript,变量未被赋值...为什么?

Javascript,变量未被赋值...为什么?

问题描述:

这是我最简单的代码:Javascript,变量未被赋值...为什么?

var this_version =null; 

this.timervar = setTimeout(function() { 
    try { 
     // Firefox 4 and later; Mozilla 2 and later 
     Components.utils.import("resource://gre/modules/AddonManager.jsm"); 
     AddonManager.getAddonByID("[email protected]", function(addon) { 
      this_version = addon.version; 
      alert("r "+this_version); 
     }); 
    } catch (ex) { 
     // Firefox 3.6 and before; Mozilla 1.9.2 and before 
     var em = Components.classes["@mozilla.org/extensions/manager;1"] 
      .getService(Components.interfaces.nsIExtensionManager); 
     var addon = em.getItemForID("[email protected]"); 
     this_version = addon.version; 
     alert("rr " + this_version); 
    } 
    alert("rrr " + this_version); 
}, 2000); 

及其该死奇怪,因为这个闪光两次alert("r "+this_version);但这alert("rrr "+this_version);总是给我空;(

仍在学习JS,所以这是非常令人费解.. 。请大家帮忙!

谢谢!

+1

当格式化为 – qwertymk

+0

ok,固定格式 – Ryan

+0

那么没有人会打扰你看看你的代码。并且在单个r的警报之前是否发生了三重r的警报? –

好,解决它通过只移动所有的东西除了警报出计时器:

var this_version =null; 
try { 
    // Firefox 4 and later; Mozilla 2 and later 
    Components.utils.import("resource://gre/modules/AddonManager.jsm"); 
    AddonManager.getAddonByID("[email protected]", function(addon) { 
     this_version = addon.version; 
     alert("r "+this_version); 
    }); 
} catch (ex) { 
    // Firefox 3.6 and before; Mozilla 1.9.2 and before 
    var em = Components.classes["@mozilla.org/extensions/manager;1"] 
     .getService(Components.interfaces.nsIExtensionManager); 
    var addon = em.getItemForID("[email protected]"); 
    this_version = addon.version; 
    alert("rr " + this_version); 
} 

this.timervar = setTimeout(function() 
          { 



            alert("rrr "+this_version); 


          }, 2000); 
+0

这不是一个可靠的解决方案。等待2秒并不能保证已经设置了'this_version'。 –

你的代码设置this_version变量是回调里面,所以对变量的变化将不会在反映您的第三个alert执行的范围。

+0

那么,如何将这个值存入我的变量中,以便我可以在其他地方使用它? – Ryan

+1

这不是范围问题,而是时间问题。假设代码在全局上下文中运行,所有函数都访问相同的* this_version *变量。即使外部上下文是函数代码,它们也会对同一个实例有一个闭包,因为它们的作用域链上没有其他* this_version *。问题在于给定给* setTimeout *的函数在其他函数设置了值之前运行,因此在运行时,设置该值的函数尚未被调用。 – RobG

+1

@Ryan - 大概*组件*正在发出异步的AJAX请求(因此是计时问题)。您可以通过调用同步来解决该问题,但这可能不是您想要的。 – RobG

这里是做在JavaScript世界的正确方法:

function done() { 
    alert("rrr "+this_version); 
} 

var this_version =null; 
try { 
    // Firefox 4 and later; Mozilla 2 and later 
    Components.utils.import("resource://gre/modules/AddonManager.jsm"); 
    AddonManager.getAddonByID("[email protected]", function(addon) { 
     this_version = addon.version; 
     alert("r "+this_version); 
     done(); 
    }); 
} catch (ex) { 
    // Firefox 3.6 and before; Mozilla 1.9.2 and before 
    var em = Components.classes["@mozilla.org/extensions/manager;1"] 
     .getService(Components.interfaces.nsIExtensionManager); 
    var addon = em.getItemForID("[email protected]"); 
    this_version = addon.version; 
    alert("rr " + this_version); 
    done(); 
} 

甚至更好:

function done(version) { 
    alert("rrr "+version); 
} 

然后调用它像这样:

done(this_version);