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