模块化代码中,callback函数如何使用全局的this变量?
代码基本结构如图~
bindLeftEvent作为initLeftMenu的callback函数,但是bindLeftEvent中this已经不是整个代码块中的this,要如何使用call、apply或者其他方法使得bindLeftEvent中的this为全局的this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两种方法,一种是比较通用的方法,使用在外部定义个this变量,然后使用匿名函数,再使用apply就可以了,入下:
第二种是es5才提供的新方法 ,使用Function.prototype.bind方法进行绑定,如下
不是可以传进去么?