extjs中为什么使用“var me = this”?

本文解释了在ExtJs中为何需要使用var me=this来保存当前对象的引用。当对象的方法调用包含回调函数时,由于回调函数的作用域问题,直接使用this将无法正确指向原始对象。因此,需要提前将this赋值给me变量,以便在回调函数中通过me来引用原始对象。

在ExtJs例子里看到大量“var me = this”,上网找了下,在:http://www.sencha.com/forum/showthread.php?136318-Why-use-quot-var-me-this-quot 找到答案:

Say you have a method in your object A which itself makes an ajax request which has a callback. In this callback you want to use a property "blob" of your original object A. You can not use "this.blob" in this callback because "this" does not refer to your object A anymore (Your scope is in the ajax request). For this reason you save "this" to a variable "me" before you call the function and then refer to "me.blob" to reference the porperty A.blob.

 

就是说当你用到回调函数的时候是无法使用this的,所以要先用me来保存this指针。

引用:http://hi.baidu.com/antzhi/item/8f5c634dd9a221eda5c06608

转载于:https://www.cnblogs.com/july2012/archive/2012/06/18/2553226.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值