Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
How to add property to object with constructor to be used by listener method?

I have a module function which sets up an addEventListener function and currently uses a global variable in the addEventListener function. I want to encapsulate this in an Object. But I can't see how to access the property from within the addEventListener function because 'this' within the addEventListener doesn't refer to my object ?

i.e

 let x=0;
 function mine (){
    window.addEventListener('click',listener);
    function listener(){
         dosomething with x;
    }
 } 
 function setX(y) {x=y};
 export {setX};

to

function Mine() {
  this.x=0;
  window.addEventListener('click',listener);
  function listener(){
    dosomething with this.x;
  }
}
Mine.prototype.setX = function(y) {this.x=y);
export {Mine};

the latter does not work because in a listener 'this' does not refer to 'Mine' (I think).

Any help appreciated.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!