0

In the following code I would like to pas myVar variable to editNote function :

class MyNotes {
  constructor() {
    this.events();
  }

  events() {
    let myVar = 5;
    document.getElementById('btn').onclick = this.editNote;
  }

  editNote() { 

  }

}

new MyNotes;

I know that if I use

events() {
  let myVar = 5;
  document.getElementById('btn').onclick = function() {};
}

I would be able to use it directly but for some reason I can't do this. Can someone help me out ?

user123456789
  • 424
  • 4
  • 15

1 Answers1

0

The question is a bit unclear, but is this what youre looking for?

class MyNotes {
  constructor() {
    this.events();
  }

  events() {
    let myVar = 5;
    document.getElementById('btn').onclick = ()=>{this.editNote(myVar)};
  }

  editNote(myVar) { 

  }

}

new MyNotes;

or

class MyNotes {
  myVar;

  constructor() {
    this.events();
  }

  events() {
    this.myVar = 5;
    document.getElementById('btn').onclick=this.editNote;
  }

  editNote() { 
     //use this.myVar
  }

}
Aditya Menon
  • 744
  • 5
  • 13