var me=this作用

原来一直不理解为什么要把this赋值给变量,要用直接用this不就行了么?这个有时候真不行!因为this在不同情况下指代的对象是随时在发生变化的,比如在现在这个代码中,this在进入ajax之前指代的是调用reloadData的那个对象,比如a.reloadData,则this此时就指代a。当进入success中后,this就指代的是ajax返回的当前window,而不再是a,此时再调用a的属性或方法就取不到了,而提前把this(a)赋值给变量,在用a时可以从变量中取到。

reloadData : function(fn) {
        var me = this;
        var setting = this.setting; 
        Hq.ajax({
            url : url,
            type : 'post',
            cache : false,
            async:this.setting.asyncAutoLoad,
            data : this.setting.params,
            dataType : 'json',
            success : function(_setting) {
             setting.dataTable.setEnableEdit(setting.editable);
            }
        });
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释下面代码 addBubble: function (bubble) { var thisBubble = this.getBubble(bubble.x, bubble.y); thisBubble.color = bubble.color; }, setBubble: function (x, y, color) { this.getBubble(x, y).color = color; }, getBubble: function (x, y) { if (x < 0 || y < 0 || x > game.cellCount || y > game.cellCount) return null; return this.bubbles[y][x]; }, isEmpty: function (x, y) { var bubble = this.getBubble(x, y); return !bubble.color; }, }; var Cell = function (x, y) { this.x = x; this.y = y; } var Bubble = function (x, y, color) { this.x = x; this.y = y; this.px = game.cellWidth * (this.x + 1) - game.cellWidth / 2; this.py = game.cellWidth * (this.y + 1) - game.cellWidth / 2; this.color = color; this.light = 10; }; Bubble.prototype.draw = function () { if (!this.color) { return; } var ctx = game.ctx; ctx.beginPath(); //console.log("x:" + px + "y:" + py); var gradient = ctx.createRadialGradient(this.px - 5, this.py - 5, 0, this.px, this.py, this.light); gradient.addColorStop(0, "white"); gradient.addColorStop(1, this.color); ctx.arc(this.px, this.py, 11, 0, Math.PI * 2); ctx.strokeStyle = this.color; ctx.fillStyle = gradient; ctx.fill(); ctx.stroke(); }; Bubble.prototype.play = function () { var me = this; var isUp = true; game.play("light_" + this.x + "" + this.y, function () { if (isUp) { me.light += 3; } if (!isUp) { me.light -= 3; } if (me.light >= 30) { isUp = false; } if (me.light <= 10) { isUp = true; } }, 50); }; Bubble.prototype.stop = function () { //this.light = 10; var me = this; game.stop("light" + this.x + "" + this.y); game.play("restore" + this.x + "" + this.y, function () { if (me.light > 10) { me.light--; } else { me.light = 10; game.stop("restore" + me.x + "_" + me.y); } }, 50); }; game.start(); </script> <div style="text-align:center;"> </div> <script src="http://www.mycodes.net/js/tongji.js"></script> <script src="http://www.mycodes.net/js/youxia.js" type="text/javascript">
最新发布
06-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值