I'm working with jquery .get
function
I'm unable to access the object variables using object the javascript code is below
function cons1(x){
this.x = ++x || false;
this.objectarray1 = [];
if(this.x){
alert('x has value');
}
}
cons1.prototype.profun1 = function(){
alert(this.objectarray1[0]);
$.get('requesting url',{'parameters'},function(data){
this.objectarray1[0] = this.x;// <-- can place a value
alert(this.objectarray1[0]);// <-- alerts this.x value
}.bind(this));
}
var x=1;
var obj1 = new cons1(x);
obj1.profun1();
var y = obj1.objectarray1[0];
alert(y);// <-- gives undefined on screen
Original code:
function cons1(x){
this.x = ++x || false;
this.objectarray1 = [];
if(this.x){
alert('x has value');
}
}
cons1.prototype.profun1 = function(){
alert(this.objectarray1[0]);
$.get('requesting url',{'parameters'},function(data){
this.objectarray1[0] = this.x;// <-- can place a value
alert(this.objectarray1[0]);// <-- alerts this.x value
}.bind(this));
}
var x=1;
var obj1 = new cons1(x);
obj1.profun1();
var y = obj1.objectarray1[0];
alert(y);// <-- gives undefined on screen
Converted to :
function cons1(x){
this.x = ++x || false;
this.objectarray1 = [];
if(this.x){
alert('x has value');
}
}
cons1.prototype.profun1 = function(){
alert(this.objectarray1[0]);
$.ajax({// <-- converted to ajax run sync
url:'requesting url',
data:{'parameters'},
async : false,
success:function(data){
this.objectarray1[0] = this.x;// <-- can place a value
alert(this.objectarray1[0]);// <-- alerts this.x value
}.bind(this)
});
}
var x=1;
var obj1 = new cons1(x);
obj1.profun1();
var y = obj1.objectarray1[0];
alert(y);// <-- now gives value