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. O002、虚拟化

    参考https://www.cnblogs.com/CloudMan6/p/5233484.html   OpenStack 是云操作系统,要学习 OpenStack,首先需要掌握一些虚拟化和云计算的 ...

  2. 富文本编辑器--使用textarea即时更新文本域同步编辑器内容

    使用 textarea wangEditor 从v3版本开始不支持 textarea ,但是可以通过onchange来实现 textarea 中提交富文本内容. <div id="di ...

  3. docker下进去mysql 编写语句

    设置密码可使用 docker exec -it mysql01 bash        --mysql01:数据库名字 mysql -u root -p ALTER USER 'root'@'%' I ...

  4. 互联网安全架构之常见的Web攻击手段及解决办法

    一.Web 安全常见攻击手段 XSS(跨站脚本攻击) SQL 注入 CSRF(跨站请求伪造) 上传漏洞 DDoS(分布式拒绝服务攻击)等 二.攻击手段原理及解决方案 1.XSS攻击 原理:XSS 攻击 ...

  5. 从FBV到CBV三(权限)

    丛FBC到CBV三(权限) span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0 ...

  6. ps aux详解(进程状态说明)

    linux上进程有5种状态: 1. 运行(正在运行或在运行队列中等待) 2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号) 3. 不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有 ...

  7. Delphi 有类型文件

  8. Python package钓鱼

    Python package钓鱼   一.概述 在收录该文之后,知道创宇404安全实验室对该文中所提到的攻击方式进行跟进.整理分析原作者公布的钓鱼数据.值得一提的是,在跟进的过程中,我们发现了新的钓鱼 ...

  9. PIL:处理图像的好模块

    介绍 PIL是一个专门用来处理图像的模块,可以对图象进行各种各样的变换 打开一张图片 from PIL import Image # 调用Image下的open方法,即可打开一张图片 # 得到的im便 ...

  10. 牛客练习赛46 A 华华教奕奕写几何 (简单数学)

    链接:https://ac.nowcoder.com/acm/contest/894/A 来源:牛客网 华华教奕奕写几何 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K ...