第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用$ this代替$(this)是否可以提高性能?

使用$ this代替$(this)是否可以提高性能?

九州編程 2019-10-21 10:25:00
假設(shè)我有以下示例:例子一$('.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');}現(xiàn)在,可能是:例子二$('.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');}關(guān)鍵不是實(shí)際的代碼,而是使用$(this)一次/兩次/三次或更多次的時(shí)間。我是不是更好的性能,明智使用例如兩個(gè)比例如一個(gè)(也許與解釋為什么或者為什么不)?編輯/注意我懷疑兩個(gè)更好。我有點(diǎn)擔(dān)心的是$this,我不可避免地忘記將其添加$this到事件處理程序中時(shí),不小心引入了一個(gè)潛在的難以診斷的錯(cuò)誤,而不是無意中引入了我的代碼。那么我應(yīng)該使用var $this = $(this)還是$this = $(this)為此?謝謝!編輯正如Scott在下面指出的那樣,這被認(rèn)為是jQuery中的緩存。http://jquery-howto.blogspot.com/2008/12/caching-in-jquery.html
查看完整描述

3 回答

?
catspeake

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊

是的,絕對(duì)可以使用$this。


每次使用時(shí)$(this),都必須構(gòu)造一個(gè)新的jQuery對(duì)象,同時(shí)$this保留相同的對(duì)象以供重用。


一個(gè)性能測(cè)試表明,$(this)是顯著慢$this。但是,由于兩者都每秒執(zhí)行數(shù)百萬個(gè)操作,因此它們都不大可能產(chǎn)生任何實(shí)際影響,但是無論如何,重用jQuery對(duì)象是一種更好的做法。當(dāng)真正的性能影響出現(xiàn)是當(dāng)一個(gè)選擇,而不是一個(gè)DOM對(duì)象,反復(fù)傳遞給jQuery的構(gòu)造-如$('p')。


至于的使用var,再次始終使用var來聲明新變量。這樣,該變量將只能在聲明它的函數(shù)中訪問,并且不會(huì)與其他函數(shù)沖突。


更好的是,jQuery設(shè)計(jì)為可與鏈接一起使用,因此請(qǐng)盡可能利用這一點(diǎn)。與其聲明一個(gè)變量并多次調(diào)用該函數(shù),不如說:


var $this = $(this);

$this.addClass('aClass');

$this.text('Hello');

...將函數(shù)鏈接在一起,以不必要地使用附加變量:


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


查看完整回答
反對(duì) 回復(fù) 2019-10-21
?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

轉(zhuǎn)到帶有jQuery的頁面,然后在控制臺(tái)中運(yùn)行它。我知道這很糟糕,但是您可以看到$ 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);


查看完整回答
反對(duì) 回復(fù) 2019-10-21
?
夢(mèng)里花落0921

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊

我無法告訴您我必須重構(gòu)jQuery b / c多少次,而人們不使用緩存。


我還要補(bǔ)充一下人們需要學(xué)習(xí)的結(jié)合緩存的方法:


var $element = $("#elementId"),

    elementLength = $element.length,

    elementText = $element.text(),

    someString = "someValue",

    someInt = 0,

    someObj = null;

代替這個(gè):


var $element = $("#elementId");

var elementLength = $element.length;

var elementText = $element.text();

var someString = "someValue";

var someInt = 0;

var someObj = null;


查看完整回答
反對(duì) 回復(fù) 2019-10-21
  • 3 回答
  • 0 關(guān)注
  • 520 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)