js中 this與that
阿新 • • 發佈:2018-06-08
一份 -s javascrip 指向 ng- bin sta PC blog
$(‘#conten‘).click(function(){
//this是被點擊的#conten
var that = this;
$(‘.conten‘).each(function(){
//this是.conten循環中當前的對象
//that仍然是剛才被點擊的#conten
});
});
可以看到,this對象在程序中隨時會改變,而var that=this之後,that沒改變之前仍然是指向當時的this,這樣就不會出現找不到原來的對象。
2017年08月05日 18:28:23 閱讀數:575
js中 this與that
在JavaScript中,this代表的是當前對象。 var that=this就是將當前的this對象復制一份到that變量中。這樣做有什麽意義呢?
//綁定事件 bindEvent:function(){ var that=this; $("btn_buy").onclick=function(){ that.buy(); } $("btn_addcart").onclick=function(){ that.addShopCart(); } }
//this是被點擊的#conten
var that = this;
$(‘.conten‘).each(function(){
//this是.conten循環中當前的對象
//that仍然是剛才被點擊的#conten
});
});
可以看到,this對象在程序中隨時會改變,而var that=this之後,that沒改變之前仍然是指向當時的this,這樣就不會出現找不到原來的對象。
js中 this與that