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)的更多相关文章

  1. check environment var

    田+R cmd set XXX check environment var

  2. var t = a&&b;的问题

    var a = "avalue";var b = "bvalue";var t = a&&b;console.info(t); // bvalu ...

  3. js中的let和var

    在ES6中,应该尽量使用const和let来声明变量,而尽量避免使用var. var的缺点是它的作用域比较混乱,使用let能够保证清晰的作用域. 下面看一个小例子. var x = 3; if(x== ...

  4. 【转载】了解CSS/CSS3原生变量var

    文章转载自:鑫空间鑫生活(https://www.zhangxinxu.com/) 原文链接:http://www.zhangxinxu.com/wordpress/?p=5804 内容摘要: 在任何 ...

  5. 在开发中到底要不要用var?

    var是.net的一个语法糖,在Resharper中推荐都使用这个关键字,平常我也是经常用:但是在跟其他程序员推广使用时,他的一些考虑引发了我的深思,到底该不该使用这个关键字呢? 我使用的理由 我使用 ...

  6. javaScript中的小细节-局部作用域中的var

    javaScript中var是很神奇的,在局部作用域中,var a = b = c = 1;是不一样的,a为使用var声明的变量,而b和c则是全局下的,此类变量被称为隐式全局变量:var a = 1; ...

  7. get_object_vars($var) vs array($var)

    get_object_vars(\(var) vs array(\)var) test case class Test { public function actionGetObjectVarsVsA ...

  8. var 的使用

    List<Enterprise> epList = ViewBag.epList; foreach (var item in epList){ //todo ... } 当 List< ...

  9. 向量自回归模型VS风险价值模型(VAR&VaR)

    单从外观上看,VAR&VaR两个模型很容易混淆,但就模型方法和用处两者截然不同,R语言作为数据分析的有力工具,其函数包库中包含各种各样的统计模型.通过vars包可以调用向量自回归模型,通过Pe ...

  10. JavaScript ES6 的 let 和 var 的比较

    在JavaScript 1.7中, let 关键词被添加进来, 我听说它声明之后类似于"本地变量", 但是我仍然不确定它和 关键词 var 的具体区别. 回答: 不同点在于作用域, ...

随机推荐

  1. node + express搭建api项目

    express框架 描述 express是一个保持最小规模的灵活的 Node.js Web 应用程序开发框架,为 Web 和移动应用程序提供一组强大的功能. 安装 // 1.使用npm淘宝镜像--cn ...

  2. CentOS查看Java进程并部署jar包

    查看Java进程获取pid号:ps -ef|grep java|grep -v grep 部署Javajar包并指定输出日志文件(null不输出):nohup java -jar xx.jar > ...

  3. 小P的架构生活(下)

    小L强烈建议团队使用微服务,并极力推荐了前公司用的一套分布式事务解决方案. 小P经过反复思考查证并做了大量的尝试后,辨证地对微服务架构做了如下分析: 为什么要用微服务,微服务带来了哪些好处? 1.减少 ...

  4. Tcp之心跳包

    Tcp之心跳包 心跳包 跳包之所以叫心跳包是因为:它像心跳一样每隔固定时间发一次,以此来告诉服务器,这个客户端还活着. 事实上这是为了保持长连接,至于这个包的内容,是没有什么特别规定的,不过一般都是很 ...

  5. 关于mail mailx 以及sendmail 的理解

    最近在弄邮件告警相关的东西,接触到了mail这一块,但是发送邮件的时间看到网上的用法 yum install mailx sednmail -y 这一块很迷糊 所以决定自己研究下 首先套用官话解释: ...

  6. VMware安装CentOS7_1511 mini版本

    这次安装使用的是 CentOS7_1511_mini 如果没有点击下载 点击下载

  7. 如何删掉git版本库master分支的第一个commit

    这个操作会将库清空,一般来说在建库开始的时候操作. 适用场景: git init初始化版本库之后,提交第一个点之后发现这个点出问题了,但是此时内心如果有洁癖的话, 你会觉得不完美,很想把这个点干掉重来 ...

  8. Type Trait 和 Type Utility

    所谓Type trait,提供了一种用来处理type 属性的办法,它是个template,可在编译期根据一个或多个template实参(通常也是type)产出一个type或者value. templa ...

  9. Atcoder Regular 099 暴力区间扩张 n/dig(n)极值打表 团分割背包

    C 直接把第一次加在哪里for一遍即可 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) u ...

  10. k8s master节点添加kubectl的使用