<javascript>this不同调用模式引起的问题,函数内部调用函数this无效

forceRefresh_1:function(){
		$("#description p").remove();
		$("#description").html("<p>一个胖子的力量...</p>");
		/*alert($("#description").text());*/
		console.log($("#description").html());
		setTimeout(this.forceRefresh_2,1000);
		return;
	},
	forceRefresh_2:function(){
		$(".play_cell.active").removeClass("active");
		$(".play_cell.rabbit").removeClass("rabbit");
		$("#description p").remove();
		this.score += 500;
		console.log(this.score);
		this.e_playScore.html(this.score); 
		this.fullArr = [];
		this.nextTetris();
		return;
	},
	//judge whether levels are full -> getScore
	//if tetris touch the top layer -> gameOver
	tetrisDown:function(){
		self=this;
		clearInterval(this.timer);
		var _index;
		var ran=Math.random();
		this.turning = false;
		
		forOuter:
		for(var j = 0, jlen = this.preTetris.length; j<jlen; j++){
			_index = $.inArray(this.preTetris[j],this.cellArr);
			for(var k = _index - _index%this.cellCol, klen = _index - _index%this.cellCol + this.cellCol; k<klen; k++){
				if(!this.cellArr[k].hasClass("active")){
					continue forOuter;
				}
			}
			if($.inArray(_index - _index%this.cellCol,this.fullArr)<0) this.fullArr.push(_index - _index%this.cellCol);
		}
		
		if(this.rabbit===true){
			this.rabbit=false;
			this.level=this.tempLevel;
			$("#description p").remove();
			$("#description").html("<p>嘭!</p>");
			/*alert($("#description").text());*/
			console.log($("#description").html());
			setTimeout(this.forceRefresh_1,1000);
			return;
		}
		if(this.fullArr.length){
			this.getScore();
			return;
		}
		for(var i = 6; i<9; i++){
			if(this.cellArr[i].hasClass("active")){
				this.gameOver("GAME OVER了,没关系,人生还长嘛!");
				return;
			}
		}
		//end the game for no reason with 0.1% probability
		if(ran<0.001){
			this.gameOver("不知道为什么,GAME OVER了,人生就是这么艰难。");
			return;
		}
		this.nextTetris();
	
	},
	

关于上面代码中tetrisDown()中调用forceRefresh_1()和forceRefresh_2()后this无效的问题,可参考

js中this的四种调用模式

在这里,this应该处于函数调用模式,被绑定到全局对象而不是外部函数的this,可以通过在外部函数中定义一个变量指向this来解决,具体代码如下:





forceRefresh_1:function(){
		$("#description p").remove();
		$("#description").html("<p>一个胖子的力量...</p>");
		/*alert($("#description").text());*/
		console.log($("#description").html());
		setTimeout(self.forceRefresh_2,1000);
		return;
	},
	forceRefresh_2:function(){
		$(".play_cell.active").removeClass("active");
		$(".play_cell.rabbit").removeClass("rabbit");
		$("#description p").remove();
		self.score += 500;
		console.log(self.score);
		self.e_playScore.html(self.score); 
		self.fullArr = [];
		self.nextTetris();
		return;
	},
	//judge whether levels are full -> getScore
	//if tetris touch the top layer -> gameOver
	tetrisDown:function(){
		self=this;
		clearInterval(this.timer);
		var _index;
		var ran=Math.random();
		this.turning = false;
		
		forOuter:
		for(var j = 0, jlen = this.preTetris.length; j<jlen; j++){
			_index = $.inArray(this.preTetris[j],this.cellArr);
			for(var k = _index - _index%this.cellCol, klen = _index - _index%this.cellCol + this.cellCol; k<klen; k++){
				if(!this.cellArr[k].hasClass("active")){
					continue forOuter;
				}
			}
			if($.inArray(_index - _index%this.cellCol,this.fullArr)<0) this.fullArr.push(_index - _index%this.cellCol);
		}
		
		if(this.rabbit===true){
			this.rabbit=false;
			this.level=this.tempLevel;
			$("#description p").remove();
			$("#description").html("<p>嘭!</p>");
			/*alert($("#description").text());*/
			console.log($("#description").html());
			setTimeout(this.forceRefresh_1,1000);
			return;
		}
		if(this.fullArr.length){
			this.getScore();
			return;
		}
		for(var i = 6; i<9; i++){
			if(this.cellArr[i].hasClass("active")){
				this.gameOver("GAME OVER了,没关系,人生还长嘛!");
				return;
			}
		}
		//end the game for no reason with 0.1% probability
		if(ran<0.001){
			this.gameOver("不知道为什么,GAME OVER了,人生就是这么艰难。");
			return;
		}
		this.nextTetris();
	
	},
	






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值