Although arrow functions aren't hoisted, what you have here aren't just arrow functions - you're using class fields here, which are syntax sugar for assigning a value to the instance inside the constructor (at the beginning of the constructor, just after any super
calls). Your code is equivalent to:
class App {
constructor() {
this.animate = () => {
this.test(); // ---> here!
};
this.test = () => {
console.log('here!');
};
this.canvas = document.createElement('canvas');
// ...
}
}
It's not an issue of hoisting.
First this.animate
gets a function assigned to it. Then this.test
gets a function assigned to it. Then, eventually, after a requestAnimationFrame
, this.animate
is called.
For a more minimal example of this:
const fn1 = () => {
fn2();
};
const fn2 = () => {
console.log('fn2');
};
fn1();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…