I´m not very experienced with JavaScript.
I defined an Object with an attribute. A method of the object makes a jQuery post with another method of the object as callback. In this second method the attribute has forgotten its value, alert prints 'undefined'
var myObj=
{ attr1 : 'val1',
method1 : function()
{ $.post("__AX_getContent?edit=true").done(this.method2);
alert(this.attr1);
},
method2: function(data)
{ alert(this.attr1);
}
}
$(myObj.method1());
what am I doing wrong to have the attributes value in method2?
That is because this
in the second method is a XHR
object send by AJAX.
Try using the .bind
function prototype.
$.post("__AX_getContent?edit=true").done(this.method2.bind(this));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.