I'm currently delving into JavaScript OOP and have encountered an issue. While my code successfully works to change a class variable for a specific instance, it fails when I try to print the updated value in an event handler.
Initially setting "myname" with a default value, I later update it for a particular instance. However, when attempting to display the new value in the event handler, I still receive the default one instead.
Can anyone provide guidance on how to modify the code to address this issue?
Thank you!
function myClass () {}
myClass.prototype =
{
myname : "test",
test : function (filename)
{
var img = createSomething ();
img.container = this;
img.addEventListener('click', this.onClick);
},
onClick : function (e)
{
trace ("click: " + e.source.container.myname); // This will output "test" instead of the expected "dave"
}
};
var instance = new myClass ();
instance.myname = "dave";
instance.test();