Javascript object binding problem inside of function prototype definitions
Posted
by Arion
on Stack Overflow
See other posts from Stack Overflow
or by Arion
Published on 2010-06-10T22:16:38Z
Indexed on
2010/06/10
22:22 UTC
Read the original article
Hit count: 193
Hi all,
I am trying to figure out the right place to bind a function prototype to be called later. The full code of the example can be found here:
My javascript example is very basic:
function Obj(width, height){
this.width = width;
this.height = height;
}
Obj.prototype.test = function(){
var xhr=init();
xhr.open('GET', '?ajax=test', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
xhr.onreadystatechange = function() {
if (xhr.responseText == '403') {
window.location.reload(false);
}
if (xhr.readyState == 4 && xhr.status == 200) {
this.response = parseResponse(xhr.responseText);
document.getElementById('resp').innerHTML = this.response.return_value;
this.doAnotherAction();
}
};
xhr.send();
}
Obj.prototype.doAnotherAction = function(){
alert('Another Action Done');
}
var myo = new Obj(4, 6);
If you try to run myo.test() in Firebug, you will get the "this.doAnotherAction is not a function" response. The 2 support functions init() and parseResponse() can be found in the test.js link if you wish to view them, but should not be too relevant to this problem. I've affirmed that this.doAnotherAction() thinks "this" is the XMLHttpResponse object as expected from an instanceof test.
Can anyone help with some insight on direction with binding? Everything I've tried seems not to work!
I do use Mootools, although the library is not present in this example.
Thanks in advance,
Arion
© Stack Overflow or respective owner