function myClass(myobject){
var myclass = {};
myclass.registerEvent = function(){
$('#'+myobject.id).live('someEventThatTriggersRightAway', runMe);
};
runMe = function(){
$('#'+myobject.id).die('someEventThatTriggersRightAway', runMe);
console.log("Hello "+myobject.name);
};
return myclass;
}
var instance1 = new myClass({id:'button1',name:'MATO'});
var instance2 = new myClass({id:'button2',name:'YAMA'});
when i run this code
instance1.registerEvent();
the result is
Hello YAMA
it is as if the first instance had been overwritten by the 2nd one. I can solve this if only i can pass the ‘myobject’ in the event’s callback, but i dont know how to do it. and also i cant kill it if it has parameters. how can i do this?
Thank you!
You have missed the
varfromrunMe, sorunMeis leaking to the global execution context. Therefore, each time you callmyClass, the reference tomyobjectinrunMeis going to change.Here’s a working example.
Side note: are you aware that the jQuery
.live()and.die()methods are deprecated? You should be using.on()(jQuery 1.7+) or.delegate()instead.