- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有以下示例:
示例一
$('.my_Selector_Selected_More_Than_One_Element').each(function() {
$(this).stuff();
$(this).moreStuff();
$(this).otherStuff();
$(this).herStuff();
$(this).myStuff();
$(this).theirStuff();
$(this).children().each(function(){
howMuchStuff();
});
$(this).tooMuchStuff();
// Plus just some regular stuff
$(this).css('display','none');
$(this).css('font-weight','bold');
$(this).has('.hisBabiesStuff').css('color','light blue');
$(this).has('.herBabiesStuff').css('color','pink');
}
现在,可能是:
示例二
$('.my_Selector_Selected_More_Than_One_Element').each(function() {
$this = $(this);
$this.stuff();
$this.moreStuff();
$this.otherStuff();
$this.herStuff();
$this.myStuff();
$this.theirStuff();
$this.children().each(function(){
howMuchStuff();
});
$this.tooMuchStuff();
// Plus just some regular stuff
$this.css('display','none');
$this.css('font-weight','bold');
$this.has('.hisBabiesStuff').css('color','light blue');
$this.has('.herBabiesStuff').css('color','pink');
}
重点不是实际的代码,而是当$(this)
使用超过一次/两次/三次或更多次时的使用。
使用示例二比使用示例一在性能方面是否更好(也许可以解释为什么或为什么不)?
编辑/注释
我怀疑两个更好;我有点担心的是在我的代码中添加了 $this
,而不是当我不可避免地忘记将 $this
添加到时无意中引入了一个潜在的难以诊断的错误一个事件处理程序。那么我应该使用 var $this = $(this)
还是 $this = $(this)
来实现此目的?
谢谢!
编辑
正如 Scott 在下面指出的,这被认为是 jQuery 中的缓存。
http://jquery-howto.blogspot.com/2008/12/caching-in-jquery.html
贾里德
最佳答案
是的,一定要使用$this
。
每次使用 $(this)
时都必须构造一个新的 jQuery 对象,而 $this
保留相同的对象以供重用。
一个performance test显示$(this)
明显比$this
慢。然而,由于两者每秒执行数百万次操作,因此不太可能产生任何真正的影响,但无论如何重用 jQuery 对象是更好的做法。当选择器(而不是 DOM 对象)被重复传递给 jQuery 构造函数时,真正会产生性能影响 - 例如$('p')
.
至于var
的使用,再次总是使用var
来声明新变量。通过这样做,变量只能在声明它的函数中访问,并且不会与其他函数冲突。
更好的是,jQuery 被设计为与链接一起使用,因此请尽可能利用这一点。而不是声明一个变量并多次调用它的函数:
var $this = $(this);
$this.addClass('aClass');
$this.text('Hello');
...将函数链接在一起,无需使用附加变量:
$(this).addClass('aClass').text('Hello');
关于javascript - 使用 $this 代替 $(this) 是否可以提高性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5724400/
我可以只用 JavaScript 编写我的网站,并确保我的代码对任何人隐藏吗?在这方面,Node.js 是否可以像 Apache 一样通过互联网提供商访问? 最佳答案 您的两个问题的答案都是是。 No
正文应仅包含 bool 而不是 json 对象或数据。 我已经尝试将 bool 转换为 JSON 中的类型。 request.httpMethod = "PUT" let sessio
假设我们有这个html内容,我们愿意用正则表达式得到Content1, Content2,.. Content1 Content2 Content3 Content4 如果我使用下面的行 preg_m
1、LUA获取utf8字符串长度 复制代码 代码如下: --- 获取utf8编码字符串正确长度的方法 -- @param str -- @return number f
我刚刚观察到 if 而不是 -> , 我写 =>在函数的类型签名定义中,它不会导致编译时错误。示例代码: mysum :: Num a => [a] => a -- Notice => after t
所以我试图替换字符串中的任何非字母数字字符,包括空格。我找到了一个可行的解决方案,但感觉很糟糕。我不需要两个单独的替换函数来完成此操作,但我不知道如何正确合并它们。我在网上找到的所有文档都没有解决这个
我有一个字符串 'abc.132131.001.3' 。我想将每次出现的 '.' 替换为 '~'. 我用过 str.replace(/[.*?^${}()|[\]\\]/g, "\~$&"); 但是这
我有这个; let subs = []; for ( const item of items ) { // array for ( const sub of item ) { //
考虑下面来自 this AngularJS tutorial 的代码片段: app.factory('Auth', function ($firebaseSimpleLogin, FIREBASE
出于培训原因,我想编写一个小计算器。为什么要计算 10-6 = 16 而不是 10-6 = 4? 我得到了错误: Assertion Failed! Expression: calc("10-6")
代码如下: /// <summary> /// 将指定字符串按指定长度进行剪切, &nbs
假设我有以下示例: 示例一 $('.my_Selector_Selected_More_Than_One_Element').each(function() { $(this).stuff()
自 Flutter 1.12 发布以来,我的以下代码用于重新启动应用程序: final MyAppState state = context.ancestorStateOfType(const Typ
这行是什么意思: bool operator() (const song& s); I am not able to understand that line with operator. Is op
我在使用 mimetype="text/plain"的 django 模板时遇到了一些问题。 首先,url 的 s3 部分以 :80 结尾,然后实际图像 url 以 '%2f' 代替每个斜杠呈现。 o
目前,如果任意(OR)条件为true,.is()的结果将返回true,如何我是否让它使用AND,即仅在满足所有条件时返回true? if ($('#search-form #valid_only').
我用 C 语言创建了一个非常简单的链表程序。 #include #include int main(){ struct Int{ int num; struct
我有以下无法更改的 HTML 输出: link1;;;link 我怎样才能摆脱;所以结果变成: 链接1;链接2 这是我最好的尝试: var test = new String($(this).html
我有以下查询,它给出了正确的结果,但我想使用不存在而不是不存在。 select cust_name from customer where cust_id not in (select c
我使用 SilverStripe 3.5.6 进行自定义搜索,它将所有关键字分解为一个数组,并且仅返回包含所有单词的结果,而不返回包含其中一个单词的结果。 这只是脚本的一小部分,但这就是我使用过滤器功
我是一名优秀的程序员,十分优秀!