简体   繁体   English

无法在对象的函数调用中更新Javascript对象属性

[英]Unable to update Javascript Object property in Object's function call

I have a CONTACT javascript object. 我有一个CONTACT javascript对象。 I call CONTACT.load() to read the data for CONTACT.id through $.ajax(). 我调用CONTACT.load()通过$ .ajax()读取CONTACT.id的数据。 On success, I am able to use the data returned by the ajax() call. 成功后,我就可以使用ajax()调用返回的数据。 Then I save some of the read data in object properties. 然后,我将一些读取的数据保存在对象属性中。 However, the saved property values are lost. 但是,保存的属性值将丢失。 Here is my code: 这是我的代码:

var CONTACT=
{
id: 1,
referrals_loaded: false,
city: '',
};



CONTACT.load = function(cb)
{
$.ajax({
        type: "POST",
        url: "contactAjax.php",
        data: { 
            ContactID: this.id,
            actionID: 'LOAD_CONTACT_DATA',
        },
        dataType: "json",
        success: function(data) {
            success = true;
            var address = data['address'];
            var addr = address[0];

            this.city =  addr['city'].trim();
            console.log("City (in ajax() ):" +this.city);
            var province = addr['province'].trim();
            // ... 


            if (typeof cb==='function') (cb)();
        },
        error: function () {

            alert("Could not load Contact data through LOAD_CONTACT_DATA .");
        }
    });

console.log("City (after ajax() ):" +this.city);

}

My calling code is like this: 我的调用代码是这样的:

CONTACT.id = 123456;
CONTACT.load('testtest');

function testtest()    {
   console.log("Contact city is " + CONTACT.city);  
   CONTACT.city = "London";
   console.log("Contact city is " + CONTACT.city);  
}

And the console.log O/P is like this: 而console.log O / P是这样的:

City (in ajax() ):MARKHAM  
Contact city in testtest()  
Contact city is London

Notice that when I set the value for CONTACT.city again in testtest(), the object retains the property value. 请注意,当我再次在testtest()中设置CONTACT.city的值时,该对象将保留属性值。 Could someone please explain, why the CONTACT.city becomes empty when testest() is called? 有人可以解释一下,为什么调用testest()时CONTACT.city为空?

'this' refers to a different object. “ this”是指另一个对象。

CONTACT.load = function(cb)
{
var self = this; // common hack
$.ajax({
        type: "POST",
        url: "contactAjax.php",
        data: { 
            ContactID: this.id,
            actionID: 'LOAD_CONTACT_DATA',
        },
        dataType: "json",
        success: function(data) {
            success = true;
            var address = data['address'];
            var addr = address[0];

            self.city =  addr['city'].trim();  // <-- assign to correct object
            console.log("City (in ajax() ):" +this.city);
            var province = addr['province'].trim();
            // ... 


            if (typeof cb==='function') (cb)();
        },
        error: function () {

            alert("Could not load Contact data through LOAD_CONTACT_DATA .");
        }
    });

console.log("City (after ajax() ):" +this.city);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM