javascript-ajax方法:刷新setter

javascript-ajax方法:刷新setter,javascript,getter-setter,Javascript,Getter Setter,我有一个如下javascript对象: var UsersControl = { users: null, fetchData: function() { $.ajax({ type: "GET", dataType: "json", context: this, url: "../php/client/json.php", data: {

我有一个如下javascript对象:

var UsersControl = {
    users: null,
    fetchData: function() {
        $.ajax({
            type: "GET",
            dataType: "json",
            context: this,
            url: "../php/client/json.php",
            data: {
                type: "users"
            }
        }).done(function(response) {
            this.users = response;
        });
    },
    getData: function() {
        if (this.users == null) {
            this.fetchData();
        }
        return this.users;
    }
};
当我运行
getData()
方法时,
users
字段已经定义,但是在这个调用中返回了旧值(
null
)。在接下来的调用中,将返回相应的值(ajax-ed)。javascript控制台会话示例:

> UsersControl
> Object {users: null, fetchData: function, getData: function}

> UsersControl.getData()
> null

> UsersControl
> Object {users: Array[2], fetchData: function, getData: function}

> UsersControl.getData()
> [Object, Object]

看起来javascript在调用方法时会记住对象的当前状态,所有状态更改都将在方法完成后应用。但这使得动态运行修改对象字段变得不可能(就像我以前使用的所有OOP语言一样)。或者,简单地说,我有一个错误的地方。如果您能给我一些关于这个话题的提示,我将不胜感激。

愚蠢的我。。。这是一个同步性的问题

fetchData: function() {
    $.ajax({
        type: "GET",
        dataType: "json",
        context: this,
        url: "../php/client/json.php",
        async:   false,               // <- note this
        data: {
            type: "users"
        }
    }).done(function(response) {
        this.users = response;
    });
},
fetchData:function(){
$.ajax({
键入:“获取”,
数据类型:“json”,
背景:这,,
url:“../php/client/json.php”,

async:false,//aah,我使用了很少的
控制台。log
查看在AJAX调用完成之前返回
fetchData
。我可以做什么使
fetchData
方法等待AJAX调用完成?