构造函数中使用 setinterval 的问题
在构造函数中使用 setinterval 时可能会遇到指向问题。当 this 指向改变时,setinterval 将不再正确地调用方法。解决方法包括:
1. 使用 bind 方法
bind方法创建一个新函数,并指定新函数的 this 关键词:
_this.circle.bind(this)
登录后复制
2. 使用箭头函数
箭头函数始终继承其外层的 this 指向:
setInterval(() => { _this.circle() })
登录后复制
以上就是在构造函数中使用 setInterval 时,如何解决 this 指向问题?的详细内容,更多请关注GTHOST其它相关文章!