javascript-can';t在两个类实例之间传递值

javascript-can';t在两个类实例之间传递值,javascript,Javascript,我已经定义了一个根对象,我想用它作为其余类的“名称空间”。在这个根对象中,我有两个类-工具和表示。在PRESENTATION类中,我需要从TOOLS调用一个公共方法。在执行此代码的每一步中,我都可以看出,返回xhr.responseText不会将任何内容传递回tools.getData(configPath)并且我在console.log(pres.config)中得到了未定义的 代码: // Create Namespace var AppSpace = AppSpace || {}; /

我已经定义了一个根对象,我想用它作为其余类的“名称空间”。在这个根对象中,我有两个类-工具和表示。在PRESENTATION类中,我需要从TOOLS调用一个公共方法。在执行此代码的每一步中,我都可以看出,
返回xhr.responseText
不会将任何内容传递回
tools.getData(configPath)
并且我在
console.log(pres.config)
中得到了
未定义的

代码:

// Create Namespace
var AppSpace = AppSpace || {}; 

// Class and Constructor
AppSpace.Tools = function() {

        //public methodts
        this.test = function(arg) {
            return arg                      
        }

        this.getData = function(path) {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', path, false);
            xhr.onreadystatechange = function() {
                if (xhr.readyState !== 4) return;
                if (xhr.status !== 0 && xhr.status !== 200) {
                    if (xhr.status === 400) {
                        console.log("Could not locate " + path);
                    } else {
                        console.error("app.getData " + path + " HTTP error: " + xhr.status);
                    }
                    return;
                }
                return xhr.responseText;
              };
              xhr.send();
        } 

}


// Class and Constructor
AppSpace.Presentation = function(initName, initfPath){

        //private properties
        var configPath = initfPath || 'config.json';
        var configData = null;
        var name = initName || 'Unnamed Presentation';  

        //private methods
        var getConfigContent = function() {
            return tools.getData(configPath);
        }

        var getConfigData = function() {
            return JSON.parse(getConfigContent);
        }


        //public methodts


        //public properties
        this.name = null;
        this.config = null;
        this.debug = null;

        //logic
        this.name = name;
        this.config = getConfigContent();


}

//execution
var tools = new AppSpace.Tools();               
var pres = new AppSpace.Presentation('Some Name');

pres.debug = tools.test('value passed')
console.log(pres.debug);
console.log(pres.config);
console.log(pres.name);
浏览器控制台中的输出为:

value passed js-oop.dev:99
**undefined js-oop.dev:100**
Some Name js-oop.dev:101

有人能给点建议吗?TIA.

我的意思是,如果希望ajax控件直接从函数返回一些数据,那么必须使用同步方法。没有它,数据将从onreadystatechange事件发送

下面是一个如何为ajax创建同步调用的示例

// Create Namespace
        var AppSpace = AppSpace || {}; 

        // Class and Constructor
        AppSpace.Tools = function() {

                //public methodts
                this.test = function(arg) {
                    return arg                      
                }

                this.getData = function(path) {
                    var xhr_object= new XMLHttpRequest();
                    // Start synchronous ajax
                    xhr_object.open("GET", path, false);
                    xhr_object.send(null);
                    // Return data
                    return xhr_object.responseText;
                } 

        }


        // Class and Constructor
        AppSpace.Presentation = function(initName, initfPath){

                //private properties
                var configPath = initfPath || 'config.json';
                var configData = null;
                var name = initName || 'Unnamed Presentation';  

                //private methods
                var getConfigContent = function() {
                    return tools.getData(configPath);
                }

                var getConfigData = function() {
                    return JSON.parse(getConfigContent);
                }


                //public methodts


                //public properties
                this.name = null;
                this.config = null;
                this.debug = null;

                //logic
                this.name = name;
                this.config = getConfigContent();


        }

        //execution
        var tools = new AppSpace.Tools();               
        var pres = new AppSpace.Presentation('Some Name');

        pres.debug = tools.test('value passed')
        console.log(pres.debug);
        console.log(pres.config);
        console.log(pres.name);​
首先,在本准则中:

this.getData = function(path) {
                    var xhr = new XMLHttpRequest();
                    xhr.open('GET', path, false);
                    xhr.onreadystatechange = function() {
                        if (xhr.readyState !== 4) return;
                        if (xhr.status !== 0 && xhr.status !== 200) {
                            if (xhr.status === 400) {
                                console.log("Could not locate " + path);
                            } else {
                                console.error("app.getData " + path + " HTTP error: " + xhr.status);
                            }
                            return;
                        }
                        return xhr.responseText;
                      };
                      xhr.send();
                } 
返回xhr.responseText将不起作用。它位于处理函数内部,值将从
xhr.onreadystatechange
返回,而不是从
getData
返回,因此您可以执行以下操作:

this.getData = function(path) {
                        var res;
                        var xhr = new XMLHttpRequest();
                        xhr.open('GET', path, false);
                        xhr.onreadystatechange = function() {
                            if (xhr.readyState !== 4) return;
                            if (xhr.status !== 0 && xhr.status !== 200) {
                                if (xhr.status === 400) {
                                    console.log("Could not locate " + path);
                                } else {
                                    console.error("app.getData " + path + " HTTP error: " + xhr.status);
                                }
                                return;
                            }
                            res = xhr.responseText;
                          };
                          xhr.send();
                          return res;
                    } 
此外,这应该与下一步类似(您正在尝试解析函数,而不是它返回的内容)


如果你想保持异步,你可以这样做。我要么删除dest和prop参数,要么删除回调,这取决于您的需要

    this.getData = function(path, dest, prop, callback) {
        callback = callback || null;
        var xhr = new XMLHttpRequest();
        xhr.open('GET', path, false);
        xhr.onreadystatechange = function() {
            if (xhr.readyState !== 4) return;
            if (xhr.status !== 0 && xhr.status !== 200) {
                /* ... */
            }
            dest[prop] = xhr.responseText;
            if (Callback) callback(xhr.responseText);
          };
          xhr.send();
    } 

    //private methods
    var getConfigContent = function() {
        return tools.getData(configPath, this, 'config', );
    }

XMLHttpRequest是一个异步调用。那么你的函数就不能返回它的值嘿,伙计,TIA是什么意思?提前感谢?是的,TIA的意思是“提前感谢”:)Gilles Hemmerlé但是当我把
console.log(xhr.responseText)
放在
return xhr.responseText
之前时,它会显示
config.json
内容。你能告诉我如何解决这个问题吗?请检查JSON.parse(getConfigContent);我认为应该有一个调用:“getConfigContent()”
    this.getData = function(path, dest, prop, callback) {
        callback = callback || null;
        var xhr = new XMLHttpRequest();
        xhr.open('GET', path, false);
        xhr.onreadystatechange = function() {
            if (xhr.readyState !== 4) return;
            if (xhr.status !== 0 && xhr.status !== 200) {
                /* ... */
            }
            dest[prop] = xhr.responseText;
            if (Callback) callback(xhr.responseText);
          };
          xhr.send();
    } 

    //private methods
    var getConfigContent = function() {
        return tools.getData(configPath, this, 'config', );
    }