亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

節點.js。如何理解 s1 = s2 === s3 和 s1 && s2?

節點.js。如何理解 s1 = s2 === s3 和 s1 && s2?

蝴蝶不菲 2022-10-27 14:40:19
我正在嘗試修復代碼,但我停在兩行奇怪的代碼上,我無法理解它們。所有行://Extraction of urlslet f = !this.last_product_urlfor(const productLink of productLinks) {    const url = await productLink.getAttribute('href')    if(!f) {        f = url === this.last_product_url        f && productUrls.push(url)    }    else {        productUrls.push(url)    }}這兩行有什么作用:f = url === this.last_product_urlf && productUrls.push(url)
查看完整描述

5 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

f = url === this.last_product_url將結果分配url === this.last_product_url給 f。

f && productUrls.push(url) 如下:

if(f) productUrls.push(url)


查看完整回答
反對 回復 2022-10-27
?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

從語法上講,這就是發生的事情:

f = url === this.last_product_url

檢查變量和分配給之間的url嚴格this.last_product_url相等f


f && productUrls.push(url)

如果ftrue,推urlproductUrls

這工作如下。該語句A && B被評估,但B僅檢查是否A為真,因為如果A為假,A && B則永遠不會為真。因此,如果A為真,則B檢查:url 被推送。


查看完整回答
反對 回復 2022-10-27
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

f = url === this.last_product_url
f && productUrls.push(url)

這兩行代碼是表示以下邏輯的緊湊方式:

if(url === this.last_product_url){
      productUrls.push(url);}


查看完整回答
反對 回復 2022-10-27
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

兩條線在做


f = (url === this.last_product_url);

if (f) {

  productUrls.push(url);

}

循環體可以通過編寫來澄清


let f = !this.last_product_url;

for (const productLink of productLinks) {

    const url = await productLink.getAttribute('href')


    if (!f) {

        f = (url === this.last_product_url);

    }

    if (f) {

        productUrls.push(url);

    }

}

但是這個奇怪f的標志真正做的是從productLinkswhere 之后獲取所有 url url === this.last_product_url。所以整個事情可能應該寫成


const allProductUrls = await Promise.all(productLinks.map(productLink =>

    productlink.getAttribute('href');

));

const lastIndex = this.last_product_url 

  ? allProductUrls.indexOf(this.last_product_url)

  : 0;

if (lastIndex > -1) {

    productUrls.push(...allProductUrls.slice(lastIndex));

}


查看完整回答
反對 回復 2022-10-27
?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

f = url === this.last_product_url相當于


if (url === this.last_product_url) {

 f = true;

} else {

 f = false;

}


f && productUrls.push(url)相當于


if (f) {

 productUrls.push(url)

}


查看完整回答
反對 回復 2022-10-27
  • 5 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號