View Full Version : ajax within object, undefined

11-11-2010, 04:25 PM
Edit: digged out some old code , added blue lines, explanation ?

comments = new Comments("some text")

function Comments(jsp)
this.jsp = jsp;
var that = this;
this.save = function(obj)
alert(this.jsp);// pops out 'some text'
///call ajax ... catch response in catch_save


this.catch_save = function()
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
alert(this.jsp);// pops out undefined
alert(that.jsp);// pops out 'some text' }


how come that there is undefined ?

11-11-2010, 06:49 PM
this refers to the current object so its value is dependent on where it's used. catch_save isn't called with the new keyword so this refers to nothing. Obviously if you did var inst=new catch_save();, this would refer to inst, but that's not what you want...