javascript - internal ajax call from object is clearing member variables -


i have object

var myobject = (function (){       var myobject = function (a,b){           this.a = a;           this.b = b;           this.c;        }       myobject.prototype.publicfunction = function(){            var somevariable = 123; //this.a , this.b both fine here.           var self = this;           $.ajax({             type: "post",             url: "default.aspx/pagemethod",             data: "{" + args + "}",             datatype: "json",             async: true,             cache: false,             contenttype: "application/json; charset=utf-8",             success: function (data, status) {                //this.a = undefined, this.b = undefined, this.c = data.              self.c = data.d              },             error: function(xhr, status, error){               alert('tears');              }        });      }   return myobject; }()); 

as enter prototype function this.a\b both values constructor. after ajax call executed this.a\b both undefined. i'm not sure here. don't understand scope in objects need to. can help?

similar question earlier, success function getting executed without context (and therefore in global context this == window)

just try logging this (console.log(this)) inside success function - you'll see it's window object.

a common workaround problem you're having create local reference this following:

 myobject.prototype.publicfunction = function(){      var self = this;       var somevariable = 123; //this.a , this.b both fine here.       //ajax hooplah       success(data) {           self.c = data.d            //this.a = undefined, this.b = undefined, this.c = data.       }       fail{         alert('tears');       }   } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -