I have the following simple JavaScript βclassβ:
function Person(params) { this.name = params.name; } Person.prototype.SayHi = function() { alert(this.name + " says hi"); }
This works as expected when I run it in place. Executing the following code gives me a popup that says: "Alice says hi":
var alice = new Person({name:"Alice"}); alice.SayHi();
But when I try to assign it to a button event, this will not work:
$("#AliceOnClick").on("click", alice.SayHi); $("#BobOnClick").on("click", bob.SayHi);
It seems that the SayHi function is SayHi called, but the name field is null.
Minimum working example:
http://jsfiddle.net/AKHsc/1/
What am I doing wrong?
source share