节点.js。如何理解 s1 = s2 === s3 和 s1 && s2?

我正在尝试修复代码,但我停在两行奇怪的代码上,我无法理解它们。所有行:


//Extraction of urls

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

        f && productUrls.push(url)

    }

    else {

        productUrls.push(url)

    }

}

这两行有什么作用:


f = url === this.last_product_url

f && productUrls.push(url)


蝴蝶不菲
浏览 84回答 5
5回答

青春有我

f = url === this.last_product_url将结果分配url === this.last_product_url给 f。f && productUrls.push(url) 如下:if(f) productUrls.push(url)

绝地无双

从语法上讲,这就是发生的事情:f = url === this.last_product_url:检查变量和分配给之间的url严格this.last_product_url相等f。f && productUrls.push(url):如果f是true,推url到productUrls。这工作如下。该语句A && B被评估,但B仅检查是否A为真,因为如果A为假,A && B则永远不会为真。因此,如果A为真,则B检查:url 被推送。

ITMISS

f = url === this.last_product_url f && productUrls.push(url)这两行代码是表示以下逻辑的紧凑方式:if(url === this.last_product_url){       productUrls.push(url);}

SMILET

两条线在做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));}

慕尼黑的夜晚无繁华

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)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript