Adding to the answers already given. You should read up on ES6 arrow functions and their use cases
Arrow functions are a shorthand method of declaring a function and binding it to the scope it is declared in.
Say a function declared like:
class C{
foo = () => {
console.log('foo')
}
}
Roughly translates to something like:
class C{
foo(){
console.log('foo')
}
this.foo = this.foo.bind(this)
}
This causes the function to be binded to the scope it is declared in. In my example, it class C. this
, if ever used in function foo
will always point to the class object. In your case, it is declared inside a function, hence, its scope will always be the function itself.
If you declare a function normally without binding it, the this
context passed to the function will be the context of the block from which you called it from. Say you called your unbinded function in class B
.
In that case, the this
passed to your function will be pointing to the object of class B
So your problem should be solved be declaring your function without a binding. Calling your function from outside any block or class will let your function inherit global scope.