var $this = $(this)
jQuery: What’s the Difference Between $(this), $this, and this?
What about $this?
$this is a little different because it’s actually just a variable that uses the $. It has no inherent relation to jQuery.
It would be no different than JavaScript variables named $corn or $carrots.
You just was easily say var $this = “My pet cat is named Mittens.”; It’s just a variable with the dollar sign in it.
JavaScript allows characters like this in variable names.
The reason that you see use of $this inside of jQuery plugins is that often times developers in the global scope inside of their plugin will say something like:
var $this = $(this);
That way they can always have a reference to the object on which the plugin was called.
The scope of “this” will change in any methods used inside the plugin so it’s always good to have a global variable (that is, global inside the plugin scope) which you can refer to the object on which the plugin was called.
But the important thing to remember is that $this has no inherent relation to jQuery. It’s just a variable like any other.
What is the reason for var $this = this
Generally, this means a copy of this
. The thing about this
is that it changes within each function.
Storing it this way, however, keeps $this
from changing whereas this
does change.
jQuery heavily uses the magic this
value.
Consider this code, where you might need something like you are seeing:
$.fn.doSomethingWithElements = function() {
var $this = this; this.each(function() {
// `this` refers to each element and differs each time this function
// is called
//
// `$this` refers to old `this`, i.e. the set of elements, and will be
// the same each time this function is called
});
};
https://blog.csdn.net/mxt123456/article/details/53152398
在很多地方,我们都会看到
var $this = $(this)的代码,那它到底是什么意思,有什么用呢?
this其实是一个html 元素。
$this 只是个变量名,加$前缀是为说明其是个jquery对象。
而$(this)是个转换,将this表示的dom对象转为jquery对象,这样就可以使用jquery提供的方法操作。
作用:把当前对象保存起来,便于后边的使用。
var $this = $(this)的更多相关文章
- check environment var
田+R cmd set XXX check environment var
- var t = a&&b;的问题
var a = "avalue";var b = "bvalue";var t = a&&b;console.info(t); // bvalu ...
- js中的let和var
在ES6中,应该尽量使用const和let来声明变量,而尽量避免使用var. var的缺点是它的作用域比较混乱,使用let能够保证清晰的作用域. 下面看一个小例子. var x = 3; if(x== ...
- 【转载】了解CSS/CSS3原生变量var
文章转载自:鑫空间鑫生活(https://www.zhangxinxu.com/) 原文链接:http://www.zhangxinxu.com/wordpress/?p=5804 内容摘要: 在任何 ...
- 在开发中到底要不要用var?
var是.net的一个语法糖,在Resharper中推荐都使用这个关键字,平常我也是经常用:但是在跟其他程序员推广使用时,他的一些考虑引发了我的深思,到底该不该使用这个关键字呢? 我使用的理由 我使用 ...
- javaScript中的小细节-局部作用域中的var
javaScript中var是很神奇的,在局部作用域中,var a = b = c = 1;是不一样的,a为使用var声明的变量,而b和c则是全局下的,此类变量被称为隐式全局变量:var a = 1; ...
- get_object_vars($var) vs array($var)
get_object_vars(\(var) vs array(\)var) test case class Test { public function actionGetObjectVarsVsA ...
- var 的使用
List<Enterprise> epList = ViewBag.epList; foreach (var item in epList){ //todo ... } 当 List< ...
- 向量自回归模型VS风险价值模型(VAR&VaR)
单从外观上看,VAR&VaR两个模型很容易混淆,但就模型方法和用处两者截然不同,R语言作为数据分析的有力工具,其函数包库中包含各种各样的统计模型.通过vars包可以调用向量自回归模型,通过Pe ...
- JavaScript ES6 的 let 和 var 的比较
在JavaScript 1.7中, let 关键词被添加进来, 我听说它声明之后类似于"本地变量", 但是我仍然不确定它和 关键词 var 的具体区别. 回答: 不同点在于作用域, ...
随机推荐
- PHP 下载mysql数据到PHPExcel表格
第一步:先到官网(https://github.com/PHPOffice/PHPExcel)下载PHPExcel 第二步:放到第三方库 第三步: /** * 封装:信息导出 * @param $da ...
- vue路由守卫触发顺序
不同组件之间的路由跳转流程图 导航被触发(A–>B) 调用A组件内路由守卫beforeRouteLeave(to,from,next) 调用全局路由前置守卫router.beforeEach(t ...
- git 基本命令操作
配置 Git 的相关参数. Git 一共有3个配置文件: 1. 仓库级的配置文件:在仓库的 .git/.gitconfig,该配置文件只对所在的仓库有效.2. 全局配置文件:Mac 系统在 ~/.gi ...
- Java读取利用java.util类Properties读取resource下的properties属性文件
说明:upload.properties属性文件在resources下 import java.io.IOException;import java.io.InputStream;import jav ...
- 免费使用Google
这里需要借助一下`梯子`,这里有教程 点击进入 如果没有谷歌浏览器,进入下载最新版谷歌浏览器,进入下载,不要移动它的安装位置,选择默认位置, 如果已经安装了谷歌浏览器,打开赛风之后,选择设置 进行安装 ...
- 内存不足导致mysql关闭,CentOS6.5增加swap分区
某日发现mysql自动关闭了,查找错误日志发现以下错误 2017-07-14 13:07:30 5494 [Note] InnoDB: Initializing buffer pool, size = ...
- linux命令详解——lsof
lsof全名list opened files,也就是列举系统中已经被打开的文件.我们都知道,linux环境中,任何事物都是文件, 设备是文件,目录是文件,甚至sockets也是文件.所以,用好lso ...
- Windows下nginx的启动,重启,关闭功能
@echo off rem 提供Windows下nginx的启动,重启,关闭功能 echo ==================begin======================== cls :: ...
- c++ 指定目录下的文件遍历
要实现指定目录下文件的遍历需要执行一下的部分: 第一步获取当前路径的名字:(MAX_PATH是在windows定义的所有的路径名字不超过其,调用该函数会使得得到当前的目录) #include < ...
- IDEA查看JDK源代码
之前已经讲解过如何使用Eclipse查看源代码,IDEA作为一个集成开发环境越来越流行,今天学习以下如何使用Eclipse查看JDK的代码. File->Project Structure,选择 ...