为什么要使用“var me=this”这样的写法

很多人都会奇怪,为什么在Ext JS 4中会大量使用“var me=this”这样的写法,其实,在官方论坛以下地址的帖子已经给出了很好的说明:

http://www.sencha.com/forum/showthread.php?132045


帖子里提到的最主要原因是脚本的压缩问题,例如以下代码:

function doA() {
    var me = this;
    
    me.a();
    me.b();
    me.c();
    me.d();
}

function doB() {
    this.a();
    this.b();
    this.c();
    this.d();
}

压缩后:

function doA(){var a=this;a.a();a.b();a.c();a.d()};
function doB(){this.a();this.b();this.c();this.d()};

从压缩后的代码可见,使用“var me=this”的写法,压缩率更高。可以想象,在Ext JS这样类很多,且大量需要使用this关键字的框架,使用“var me=this”,确实可以大大减少压缩包的大小。
await that.connectGoEasy(); 这一行 报错 async toDetail(item) { if(item.is_me == 1){ return false } if(!global.token){ this.toLoginDiy() return false } if(!global.token || !global.userInfo){ return false } if(global.userInfo.id == item.user_id){ return false; } var that = this this.$api.post(global.apiUrls.postSwitch).then((res) => { if (res.statusCode == 200 && res.data.code == 1) { console.log(res); if(res.data.data == 1){ console.log(item.is_me); if (item.can_chat == 0) { // that.$message.info(item.can_chat_msg)//不可与自己聊天 return false } console.log(global.userInfo,'global.userInfo') uni.showLoading() // 2.都有 则登录 然后跳转聊天详情 uni.setStorageSync('currentUser', global.userInfo);//缓存存入本地登录信息 that.currentUser = global.userInfo; getApp().globalData.currentUser = global.userInfo; //聊天开始检测 连接 登录 start const connectionStatus = that.goEasy.getConnectionStatus(); console.log(connectionStatus, 'pyq点击发布者头像聊天------聊天系统登录状态'); try { // 连接状态机处理(原注释保留) if (connectionStatus === 'disconnected') { console.log('pyq点击发布者头像聊天------正在连接goEasy'); await that.connectGoEasy(); } //聊天留存接口 var nameList = { id: item.user_id,//对方id name: item.author_info.f_nickname || item.user_nickname,//聊天窗口对方昵称 avatar: item.author_info.f_head_img ||item.user_head_img, //我的头像? }; uni.navigateTo({ url: '/pages/index/friend/privateChat'+'?to=' + JSON.stringify(nameList) }); uni.hideLoading(); } catch (error) { console.error('pyq点击发布者头像聊天------连接流程异常:', error); uni.hideLoading(); } //聊天开始检测 连接 登录 end // if (that.goEasy.getConnectionStatus() === 'disconnected') { // that.connectGoEasy(); //连接goeasy // } // setTimeout(function() { // uni.hideLoading() // //聊天留存接口 // var nameList = { // id: item.user_id,//对方id // name: item.author_info.f_nickname || item.user_nickname,//聊天窗口对方昵称 // avatar: item.author_info.f_head_img ||item.user_head_img, //我的头像? // }; // uni.navigateTo({ // url: '/pages/index/friend/privateChat'+'?to=' + JSON.stringify(nameList) // }); // }, 1500) } } else { console.log("err: " + res); } }) },
最新发布
11-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值