mardi 14 juin 2016

Javsacript object reference from jquery callback [duplicate]


This question already has an answer here:

Say that i have this code

var SomeClass= function(id){
      this.id = guidGenerator();
      this.htmlElement = jQuery("#"+id);
      this.initClickHandler();
 };

 SomeClass.prototype = {
       initClickHandler: function(){
          this.htmlElement.on("click",function(e){
               //NEED A REFERENCE TO THE OBJECT HERE
               //IN A WAY THAT WILL ALLOW SOMETHING LIKE THIS
               //this.clickHandler();
          });
      },
      clickHandler: function(){
          alert(this.id);
      }
 };

SomeClassInstance1 = new SomeClass("a");
SomeClassInstance2 = new SomeClass("b");

how can i get the relevant instance of "SomeClass" on the "ON "callback ?


Aucun commentaire:

Enregistrer un commentaire