【发布时间】:2020-12-30 17:34:24
【问题描述】:
class App {
constructor() {
this.canvas = document.createElement('canvas');
document.body.appendChild(this.canvas);
this.ctx = this.canvas.getContext('2d');
this.pixelRatio = window.devicePixelRatio > 1 ? 2 : 1;
window.addEventListener('resize', this.resize.bind(this), false);
this.resize();
window.requestAnimationFrame(this.animate);
}
resize() {
this.stageWidth = document.body.clientWidth;
this.stageHeight = document.body.clientHeight;
}
animate = () => {
this.test(); // ---> here!
};
test = () => {
console.log('here!');
};
}
window.onload = () => {
new App();
};
箭头函数没有被提升,只有常规函数被提升。为什么在 animate 函数内部可以调用 this.test?类中箭头函数的不同行为?
【问题讨论】:
标签: javascript hoisting