企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 有关this的绑定 > 由于js我们不需要管理指针,而是提供一个特殊值this指向当前对象 > 所以我们经常会遇到回调函数,需要绑定其运行环境,也就是当前对像 > * 箭头函数 ~~~ () => {} ~~~ 这里不解释了 主要讲的是在阅读微信小程序的文档时看到了一个比较特别的写法 ~~~ setTimeout(function() { animation.translate(30).step() this.setData({ animationData:animation.export() }) }.bind(this), 1000) ~~~ * 在上面可以看出,在回调函数的后面直接.bind(this)就可以了 * 具体可以查看 [微信文档](https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-animation.html#wxcreateanimationobject) 浏览器console测试: ~~~ function persion () { this.keys = "kingends" setTimeout(function() {console.log(this.keys)}.bind(this), 1000)} undefined var pre = new persion() undefined VM451:3 kingends function persion () { this.keys = "kingends" setTimeout(function() {console.log(this.keys)}, 1000)} undefined var pre = new persion() undefined VM509:3 undefined ~~~ * 结果通过了。