在阅读别人的代码时,发现别人写的代码中有这么一句:var that = this;,这代表什么意思呢?经过一番查阅,才明白是这么回事。

在JavaScript中,this代表的是当前对象。

var that=this就是将当前的this对象复制一份到that变量中。这样做有什么意义呢?

1
2
3
4
5
6
7
8
$(‘#conten').click(function(){
//this是被点击的#conten
var that = this;
$(‘.conten').each(function(){
//this是.conten循环中当前的对象
//that仍然是刚才被点击的#conten
});
});

可以看到,this对象在程序中随时会改变,而var that=this之后,that没改变之前仍然是指向当时的this,这样就不会出现找不到原来的对象。

最新文章

  1. 使用httpclient 调用selenium webdriver
  2. SQL Server 2014新特性探秘(1)-内存数据库
  3. 【记录】JS 获取图片原始尺寸-防止图片溢出
  4. javascript设计模式实践之策略模式--输入验证
  5. Represent code in math equations
  6. fix eclipse gc overhead limit exceeded in mac
  7. 前端学习资源(CSS+HTML5)
  8. HackerRank "Morgan and a String"
  9. JUC之Atomic系列12大类实例讲解和原理分解
  10. 在服务器端使用 Git 创建源代码仓库
  11. 转 Android adb root权限
  12. textarea使用注意事项
  13. CI 在nginx中出现404错误的解决方式
  14. android launcher3 home页简易分析
  15. Codeforces Round #198 (Div. 2) C. Tourist Problem
  16. HDU1257 最小拦截系统 【贪婪】
  17. error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches
  18. Web平台安装及检测程序
  19. [编织消息框架][JAVA核心技术]异常应用
  20. margin塌陷与margin合并(margin),清除浮动

热门文章

  1. OnCtlColor
  2. Eclipse如何查看接口实现类快捷键
  3. 51nod - 1659 - 数方块 - 简单数学
  4. opencv 缩放旋转
  5. OPENGL_变换与坐标系
  6. MongoDb 数据修补笔记
  7. [Xcode 实际操作]九、实用进阶-(9)陀螺仪设备的使用
  8. Maven多模块构建实例
  9. Android 兼容包
  10. AVL树(自平衡二叉查找树)