I'm new to prototype based languages and have read this question:
I'm wondering what value there is, of using a prototype based signature to attach methods to an object. Why not just attach the method to the object's property in the object's definition?
Second, when using the the prototype signature to define methods on an object, why does the 'this' pointer resolve to the window object inside the function? This appears to be a design flaw. If it's not could someone explain, or point me to an explanation regarding why not?
Thank you.
Edit:
This code performs as expected, rendering a message box with the word 'here' inside.
function Obj()
{
this.theVar = 'here';
this.method2 = function(){ alert( this.theVar ); }
}
Obj.prototype.method3 = function(){ this.method2(); }
Obj.prototype.method4 = function(){ this.method3(); }
var obj = new Obj();
obj.method4();
This code is my AJAX callback, and the 'this' pointer refers to the 'window' object during execution.
Test.prototype.nextCallback = function( response )
{
if( response.Status != 'SUCCESS' )
show_message( response.Message, false );
else
this.setQuestion( response.Question );
}
Test.prototype.setQuestion = function( question ){ ... }
The 'this' pointer actually works properly before the AJAX call, but not after. Is this result because the nextCallback() context is not properly restored after the AJAX call returns and before the callback is called? Is there a way to remedy this?