1

Why am I getting the error: "TypeError: Cannot read property 'foo' of undefined"? How do I call a method from another method in a JS class?

class A {
  foo() {
    console.log("foo");
  }

  bar() {
    console.log("bar");
    this.foo();
  }
}

class B {
  call(fn) {
    fn();
  }
}

const a = new A();
const b = new B();

b.call(a.foo);
b.call(a.bar);

Nelson Mfinda
  • 139
  • 1
  • 6

0 Answers0