How to get a reference to an object inside a callback function in the object in JavaScript

1 Answer

0 votes
const worker = {
    name: "Tom",
    print() {
        setTimeout(() => {
            console.log(this);
        }, 1000);
    }
};
 
worker.print();
 
 
  
/*
run:
       
{ name: 'Tom', print: [Function: print] }
  
*/

 



answered Mar 29, 2020 by avibootz
...