Property undefined when calling a function from another class

I created this sample code to demonstrate what I’m trying to do. Run this code.

Cannot read property ‘myValue’ of undefined

class Foo {
    myValue = 'test123';
    boo: Boo;

    constructor(boo: Boo) {
        this.boo = boo;
    }

    memoFunc() {
        this.boo.anotherFunction(this.myFunction);
    }

    myFunction() {
        console.log(this.myValue);
    }
}

class Boo {
    anotherFunction(func: () => void) {
        func();
    }
}

const foo = new Foo(new Boo());
foo.memoFunc();

76 thoughts on “Property undefined when calling a function from another class”

Leave a Comment