0

In the code bellow I'm passing a method of a class instance as callback argument to a another function. But this is undefined when I pass just the method signature. So I wonder what changes this according to the callback argument format. Is there some JS docs for this specific case?

class MyClass {
    foo = 'foo';
    bar() {
        console.log('This is:');
        console.log(this);
    }
}

const handler = (callback) => {
    callback()
}

const myClass = new MyClass()

handler(myClass.bar)
handler(() => myClass.bar())

Output:

This is:
undefined

This is:
MyClass {foo: 'foo'}
Jeff Pal
  • 1,519
  • 1
  • 17
  • 28

0 Answers0