JavaScript中使用“var me=this”的总结

首先直接说结果:为什么要使用var me = this保存this指针。一、为了保存this指向,在不同情况下this的指向都是不同的。二、脚本的压缩问题。

一、JavaScript this 关键字指向

java中的this表示当前对象的一个引用。
但是JavaScript中的this不是固定不变的,它会随执行环境的改变而改变的。
1、在对象方法中,this指向调用它所在方法的对象。
2、单独使用this,它指向全局(Global)对象。
3、函数使用中,this指向函数的所属者。
4、严格模式下函数是没有绑定到this上,这时候this是undefined。
5、在 HTML 事件句柄中,this 指向了接收事件的 HTML 元素。
6、apply 和 call 允许切换函数执行的上下文环境(context),即 this 绑定的对象,可以将 this 引用到任何对象。
详细概念解释地址—菜鸟教程
这里对常用的1、方法中的this;2、单独使用this;3、事件中使用this。举例子进行解释:

1、方法中的this

<script>
// 创建一个对象
var person = {
  firstName: "John",
  lastName : "Doe",
  id     : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

// 显示对象的数据
document.getElementById("demo").innerHTML = person.fullName();
</script>

这里fullName是person对象的方法,方法中使用this指向该person对象。这里输出的结果就是John Doe

2、单独使用this

单独使用 this,则它指向全局(Global)对象。

在浏览器中,window 就是该全局对象为 [object Window]

3、事件中使用this

在HTML事件句柄中,使用this指向了接收事件的HTML元素。如果是button的onclick事件中使用this,那么this指向的就是该button元素。

<button onclick="this.style.display='none'">点我后我就消失了</button>

二、脚本压缩问题

参考:https://blog.csdn.net/iteye_20659/article/details/82524537
实际上就是var me = this保存过后的变量,在压缩之后调用时,调用者就是确定的me,而不是this指针。

//压缩之前
function doA() {
    var a = this;
    
    a.a();
    a.b();
    a.c();
    a.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()};
  • 1
    点赞
  • 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"></script> </body> </html>
最新发布
06-11

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值