访问函数中创建的javascript对象

访问函数中创建的javascript对象,javascript,ajax,variables,object,Javascript,Ajax,Variables,Object,我将使用javascript对象与php对象相结合来完成我的第一步。到目前为止,一切正常,但我很难在这个函数之外访问ajax成功响应中创建的javascript对象 以下是我的JS代码: function settings(mannstage, stundenlohn, tags) { this.mannstage = mannstage; this.stundenlohn = stundenlohn; this.tags = tags; } var currentSet

我将使用javascript对象与php对象相结合来完成我的第一步。到目前为止,一切正常,但我很难在这个函数之外访问ajax成功响应中创建的javascript对象

以下是我的JS代码:

function settings(mannstage, stundenlohn, tags) {
    this.mannstage = mannstage;
    this.stundenlohn = stundenlohn;
    this.tags = tags;
}
var currentSettings;
SendAjaxJsonRequest("getSettings");

function SendAjaxJsonRequest(method, jsonObject) {
    jsonObject = (typeof jsonObject === "undefined") ? "none" : jsonObject;
    $.ajax({
        type: "POST",
        url: "app/class/controller/ajax.php",
        data: {
            method: method,
            jsonObject: jsonObject
        },
        success: onSuccess
    })
};

function onSuccess(content) {

    var response = $.parseJSON(content);
    currentSettings = new settings(response.mannstage, response.stundenlohn, response.tags);
    console.log(currentSettings); //Returns the object
}
console.log(currentSettings); //This is undefined
最后一个console.log未定义。如何在
onSuccess
功能之外访问
currentSettings


谢谢大家!

onSuccess
函数可以在
onSuccess
之外访问
currentSettings
,但上次的
控制台.log
调用会在定义
onSuccess
后立即运行,这是在调用之前,因此
currentSettings
的值在该点上仍然是
未定义的

也许像这样编辑代码可以演示:

function onSuccess(content) {

    var response = $.parseJSON(content);
    currentSettings = new settings(response.mannstage, response.stundenlohn, response.tags);
    console.log(currentSettings); //Returns the object
    afterSuccess(); // Also returns the object
}

function afterSuccess() {
    console.log(currentSettings);
}

Ajax是异步的,因此在调用success函数之前将执行最后一个console.log。

我没有考虑Ajax的异步性。多谢各位@塞贝西米亚:不客气。“A”表示异步!(但是“X”代表“XML”,所以这个首字母缩写词可能并不特别相关。)呵呵,我知道!但现在我会记得……)