Why is the following throwing an error, and how would I go about trying to fix.
class Foo {
bar() {
console.log("bar");
}
fizz() {
this.bar(); // TypeError: this is undefined
}
}
let foo = new Foo();
let buzz = foo.fizz;
buzz();
Why is the following throwing an error, and how would I go about trying to fix.
class Foo {
bar() {
console.log("bar");
}
fizz() {
this.bar(); // TypeError: this is undefined
}
}
let foo = new Foo();
let buzz = foo.fizz;
buzz();
Use arrow function:
class Foo {
bar() {
console.log("bar");
}
fizz=()=> {
this.bar(); // TypeError: this is undefined
}
}
let foo = new Foo();
let buzz = foo.fizz;
buzz();