Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不使用window.myvariable的情况下访问javascript YUI中回调对象中的全局变量?_Javascript_Yui - Fatal编程技术网

如何在不使用window.myvariable的情况下访问javascript YUI中回调对象中的全局变量?

如何在不使用window.myvariable的情况下访问javascript YUI中回调对象中的全局变量?,javascript,yui,Javascript,Yui,我有这个: var MyObject = function(){ this.url = "monurl"; this.mavar = ""; this.Load = function(){ var callback = { success: function(o){ mavar = o.responseXML.getElementsByTagName("montag")[0].firstChild.nodeValue;

我有这个:

var MyObject = function(){   
   this.url = "monurl";   
   this.mavar = "";   
   this.Load = function(){
     var callback = {
       success: function(o){
         mavar = o.responseXML.getElementsByTagName("montag")[0].firstChild.nodeValue;
       }
     }
     YAHOO.util.Connect.asyncRequest('GET',url,callback);
} }

mavar变量不可访问。如何执行此操作?

保存
此变量:

var MyObject = function() {   
   this.url = "monurl";   
   this.mavar = "";   
   this.Load = function() {
     var me = this;
     var callback = {
       success: function(o) {
         me.mavar = o.responseXML.getElementsByTagName("montag")[0].firstChild.nodeValue;
       }
     }
     YAHOO.util.Connect.asyncRequest('GET',url,callback);
  }
}

您也可以这样做:

var MyObject = new function(){   
   this.url = "monurl";   
   this.mavar = "";   
   this.Load = 
   (function(that){ 
        return function(){
            var callback = {
                success: function(o){
                    that.mavar = o.responseXML.getElementsByTagName("montag")[0].firstChild.nodeValue;
                }
            }
            YAHOO.util.Connect.asyncRequest('GET',url,callback);
        } 
    })(this);
};

在我的对象中,我添加了一个引用如下的变量:

var selfMyObject = this;

在成功回调中,我将this.mavar替换为selfMyObject.mavar,它工作正常。

处理此问题的最佳方法是让YUI进行范围更正,因为支持是内置的。这是文档页面,告诉你怎么做


我相信您可以为回调设置一个scope参数。因此,您应该能够执行以下操作

var MyObject = function(){   
   this.url = "monurl";   
   this.mavar = "";   
   this.Load = function(){
     var callback = {
       success: function(o){
         this.mavar = o.responseXML.getElementsByTagName("montag")[0].firstChild.nodeValue;
       },
       scope: this
     }
     YAHOO.util.Connect.asyncRequest('GET', this.url, callback);
} }

第二种方法(通过对象名访问)不起作用。您正在将属性
mavar
添加到构造函数函数对象,而不是实际的
MyObject
对象实例。第一种方法是将
这个
保存到另一个变量(我喜欢称它为
\u这个
自我
),这是最好的方法。@Steve:对,编辑过。没有注意到它是一个构造函数而不是一个对象文本。对!我尝试了一下,我的选择是把它放到另一个变量。谢谢
var MyObject = function(){   
   this.url = "monurl";   
   this.mavar = "";   
   this.Load = function(){
     var callback = {
       success: function(o){
         this.mavar = o.responseXML.getElementsByTagName("montag")[0].firstChild.nodeValue;
       },
       scope: this
     }
     YAHOO.util.Connect.asyncRequest('GET', this.url, callback);
} }