为了账号安全,请及时绑定邮箱和手机立即绑定

节点.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 关注
  • 82 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信