I have the following javascript object, somewhat pseudocode:
{
dateField: new Date(),
addMinutes:function(numMinutes)
{
CallWebService(numMinutes, function{alert(this.dateField; });
}
}
The problem is the scope of the callback function in CallWebService doesn’t see the dateField property of the object. Is there a way I can access it? Thanks!
You need to preserve the context (the
thisvalue) of theaddMinutesfunction.There are several ways to achieve it, the most easy one is to simply store a reference of
thison a variable, that variable will be available to the scope of the callback function, e.g.: