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
Post a Comment