I'm not too familiar with object orientated Javascript but lets say I have a function which I instantiated.. ?
Is that the correct terminology?
The function contains an XMLHttpRequest with an attached event listener, what would be the correct way to dispose the function?
function foo() {
var self = this;
self.message = "received response";
self.url = 'someUrl';
self.req = new XMLHttpRequest();
self.req.open("POST", self.url, true);
self.req.onload = function () {
console.log(self.message); // Unable to get property 'message' of
// undefined or null reference
};
self.req.send();
self = null;
}
var foo = new foo();
I realize this is not how I should abort an XMLHttpRequest but its just an example, the question is how should I cleanly destroy a function and all that it spawned or created?
Edit
To Clarify, my confusion is with self.req.onload and how it can even exist when self = null? my thought would be that self.req no longer exists and therefore its events too but this is obviously not the case
fooshould be a constructor function. Just make a it a "normal" function and don't usethis/self.