相关疑难解决方法(0)

使用$ this代替$(this)可以提供性能提升吗?

假设我有以下示例:

例一

$('.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');
}
Run Code Online (Sandbox Code Playgroud)

现在,它可能是:

例二

$('.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');
}
Run Code Online (Sandbox Code Playgroud)

这一点不是实际的代码,而是使用$(this)何时使用超过一次/两次/三次或更多次.

使用示例二而不是示例一,我的表现更好(可能有解释原因或原因)?

编辑/ NOTE

我怀疑两个是更好的一个; $this当我不可避免地忘记添加$this到事件处理程序时,我有点担心的是用我的代码加工而不是无意中引入了一个可能难以诊断的错误.我应该使用var $this = $(this),还是$this = $(this)为此?

谢谢! …

javascript jquery caching this

17
推荐指数
1
解决办法
858
查看次数

标签 统计

caching ×1

javascript ×1

jquery ×1

this ×1