Suppose, we have the following code:
HTML:
<input type="button" id="button" value="Say hello">
Javascript:
function test ()
{
this.message = "hello";
this.on_change = function (){
alert(this.message);
}
this.on_change();
}
$(document).ready(function() {
var my_object = new test();
$('#button').click(my_object.on_change);
});
my_object must print "Hello" in 2 cases:
- When the instance is created (works)
- When clicking the button "Say hello" (doesn't work)
In the second case it prints "Undefined", because this
refers to "button" object, and not to the instance of test
class.