javascript - Passing Class Variables to Class Method in Prototype via addEventListener -


i have following class file i'm attempting build. i'd pass multiple variables method way of eventlistener code have below doesn't work, due scoping. not sure should change though. advice appreciated.

var myclass= new class.create(); myclass.prototype = {     initialize: function(id,name,top,left){         try{             this.id = id;             this.name = name;             this.currenttop = top;             this.currentleft = left;              $(id).addeventlistener("mousedown",function(event,this.id,this.name){                 this.grabobj(event,this.id,this.name);             },false);          }         catch(error){alert(error);}     },     grabobj:function(event,myid,myname){         // here myid , myname     } }; 

your syntax wrong.

instead, should make separate variable hold real this, this:

var myclass= new class.create(); myclass.prototype = {     initialize: function(id,name,top,left){         try{             this.id = id;             this.name = name;             this.currenttop = top;             this.currentleft = left;              var self = this;              $(id).addeventlistener("mousedown",function(event){                 self.grabobj(event);             }, false);          }         catch(error){alert(error);}     },     grabobj:function(event){         // here this.id , this.name     } }; 

since self.grabobj normal method call, this myclass instance.


Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -