Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用$this而不是$(this)是否可以提高性能?_Javascript_Jquery_Caching_This - Fatal编程技术网

Javascript 使用$this而不是$(this)是否可以提高性能?

Javascript 使用$this而不是$(this)是否可以提高性能?,javascript,jquery,caching,this,Javascript,Jquery,Caching,This,假设我有以下示例: 示例一 $('.my_Selector_Selected_More_Than_One_Element').each(function() { $(this).stuff(); $(this).moreStuff(); $(this).otherStuff(); $(this).herStuff(); $(this).myStuff(); $(this).theirStuff(); $(this).children().e

假设我有以下示例:

示例一

$('.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中的缓存


Jared

是的,一定要使用
$this

每次使用
$(this)
时都必须构造一个新的jQuery对象,而
$this
保留相同的对象以供重用


A显示
$(this)
$this
慢得多。然而,由于两者都在每秒执行数百万次操作,因此两者都不太可能产生任何实际影响,但最好还是重用jQuery对象。真正影响性能的是选择器而不是DOM对象被反复传递给jQuery构造函数,例如
$('p')


至于
var
的使用,同样总是使用
var
来声明新变量。这样,变量将只能在声明它的函数中访问,并且不会与其他函数冲突


更好的是,jQuery设计用于链接,因此尽可能利用这一点。与其声明变量并多次调用变量上的函数,不如:

var $this = $(this);
$this.addClass('aClass');
$this.text('Hello');
…将函数链接在一起,使附加变量的使用变得不必要:

$(this).addClass('aClass').text('Hello');

是的,如果多次引用$(this),则应将其存储在变量中$这只是一个惯例,让其他人知道$this代表一个jquery对象

如果jquery对象是使用复杂的选择器创建的,则这一点更为重要

此外,在伪代码示例中,通常可以使用$上的链接(this)

例如:

$(this).addClass("someclass").css({"color": "red"});

转到带有jQuery的页面,并在控制台中运行它。我知道这很糟糕,但你可以看到每次$this都赢了

var time = new Date().getTime();
$('div').each(function() {
  $(this).addClass('hello');
  $(this).removeClass('hello');
  $(this).addClass('hello');
  $(this).removeClass('hello');
  $(this).addClass('hello');
  $(this).removeClass('hello');
  $(this).addClass('hello');
  $(this).removeClass('hello');
  $(this).addClass('hello');
  $(this).removeClass('hello');
  $(this).addClass('hello');
  $(this).removeClass('hello');
  $(this).addClass('hello');
  $(this).removeClass('hello');
});
var now = new Date().getTime();
console.log(now- time);

var var_time = new Date().getTime();
$('div').each(function() {
  var $this = $(this);
  $this.addClass('hello');
  $this.removeClass('hello');
  $this.addClass('hello');
  $this.removeClass('hello');
  $this.addClass('hello');
  $this.removeClass('hello');
  $this.addClass('hello');
  $this.removeClass('hello');
  $this.addClass('hello');
  $this.removeClass('hello');
  $this.addClass('hello');
  $this.removeClass('hello');
  $this.addClass('hello');
  $this.removeClass('hello');
});
var var_now = new Date().getTime();
console.log(var_now - var_time);

@第九箱完全正确。我无法告诉您我有多少次需要重构jQuery b/c,因为人们不使用缓存

我还想补充一点,结合缓存,人们需要学习如何做到这一点:

var $element = $("#elementId"),
    elementLength = $element.length,
    elementText = $element.text(),
    someString = "someValue",
    someInt = 0,
    someObj = null;
与此相反:

var $element = $("#elementId");
var elementLength = $element.length;
var elementText = $element.text();
var someString = "someValue";
var someInt = 0;
var someObj = null;

还将
$this
声明为
var$this
,以便
$this
包含在
each()
范围内,而不是默认全局范围内scope@mdmullinax-我刚刚加了一条说明,要求作出同样的澄清。谢谢加油,我希望我能投两次票。编辑是锦上添花。@Jared补充了一些关于澄清问题的进一步说明。@Box9-性能测试非常有趣。在链接方面,是否仅使用
$this
就提供了性能增强?这是上面的修改版本:。当您说缓存时,您是指Box9的链接示例吗?另外,为什么在每次声明之前都有一个
var
声明不是一种性能增强呢?(我认为前者很难辨别)@Jared Farrish-@Jared Farrish-@Scott-哦,好吧,
$这个问题被认为是缓存(我会称之为存储引用,而不是跳转到缓存)。你没有回答关于使用多个
var
的问题。@Jared,
var
更多的是关于风格。对于某些人和其他人(例如Douglas Crockford),确保每个函数只有一个
var
,这一点很重要