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();
	
	},
	






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章