score:1

Accepted answer

You can make it a function and pass it down in the function you will have access to this:

class foo {

  constructor(a){
    this.a = a;

  }
  method1(){

    var referenceToMyClass = this;  
    var method2 = function(){
      //do something with referenceToMyClass
    }    
    this.dragEvent = d3.drag(referenceToMyClass)
    .on("drag", method2);

    d3.select('target id').call(this.dragEvent);
  }
}   

or you can use bind:

class foo {

  constructor(a){
    this.a = a;
    this.method2.bind(this);
  }
  method1(){

    var referenceToMyClass = this;  

    this.dragEvent = d3.drag(referenceToMyClass)
    .on("drag", this.method2);

    d3.select('target id').call(this.dragEvent);
  }
  method2(){
   //this refers to the object now.
  }

} 

Related Query

More Query from same tag