定义_this=this的作用

转自:https://www.imooc.com/qadetail/175968?t=257782

下面有一段Jquery的代码段 

$("#btn").click(function(){
   var _this = this;//这里this和_this都代表了"#btn"这个对象
   $(".tr").each(function(){
         this;//在这里this代表的是每个遍历到的".tr"对象
         _this;//仍代表"#btn"对象
   })
})

这种情况就是在一个代码片段里this有可能代表不同的对象,而编码者希望_this代表最初的对象

猜你喜欢

转载自blog.csdn.net/shelbyandfxj/article/details/82563762