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

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

在 JavaScript 中跳出 if 塊的正確方法是什么?

在 JavaScript 中跳出 if 塊的正確方法是什么?

慕碼人8056858 2021-06-29 08:26:12
在 if 塊結(jié)構(gòu)中,如下所示,假設(shè) condition_1 和 condition_2 是互斥的,但有時(shí) condition_2 和后面的條件都可以為真;并且,當(dāng) condition_2 為真時(shí),所需要做的就是跳出 if 塊并繼續(xù)執(zhí)行其余代碼,類似于 switch 語句。除condition_2 之外的所有條件都是matches針對具有多個(gè)按鈕的父容器上的偵聽器的語句。當(dāng) condition_2 為真時(shí),它下面的按鈕應(yīng)該被禁用。if ( condition_1 ) { }else if ( condition_2 ) {  }else if ( condition_3 ) {  }else if ( condition_4 ) {  }// ...else if ( condition_n ) {  };   // More code in the function before returning.可以編碼為:if ( condition_1 ) { }else if ( !condition_2 && condition_3 ) {  }else if ( !condition_2 && condition_4 ) {  }// ...else if ( !condition_2 && condition_n ) {  };   // More code in the function before returning.或者if ( condition_1 ) { }else if ( !condition_2 )  {    if ( condition_3 ) {  }    else if ( condition_4 ) {  }    // ...    else if ( condition_n ) {  };     };// More code in the function before returning.只在第一個(gè)塊中進(jìn)行編碼并且只在 condition_2 的大括號(hào)之間不放置任何代碼,這樣當(dāng) condition_2 為真時(shí),沒有代碼可以執(zhí)行,但其他條件沒有被測試,它會(huì)選擇if 塊末尾的代碼?有沒有更好更專業(yè)的方法來完成同樣的任務(wù)?我讀過關(guān)于label在 if 語句上放置 a然后使用break label,但我沒有看到添加了什么;并且有人提到編譯器/解釋器可能無法有效地使用該方法。
查看完整描述

1 回答

?
幕布斯7119047

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

如果條件是 ,您可以采用帶標(biāo)簽的語句并中斷塊語句{}true。


var a = 2;

block: {

    if (a === 1) {

        console.log(1);

        break block;

    }

    if (a === 2) {

        console.log(2);

        break block;

    }

    if (a === 3) {

        console.log(3);

        break block;

    }

    console.log('end of block');

}

或者在同一個(gè)范圍內(nèi)使用另一個(gè)嵌套函數(shù)并提前返回。


function check () {

    if (a === 1) {

        console.log(1);

        return;

    }

    if (a === 2) {

        console.log(2);

        return;

    }

    if (a === 3) {

        console.log(3);

        return;

    }

    console.log('end of function');

}


var a = 2;

check();


查看完整回答
反對 回復(fù) 2021-07-01
  • 1 回答
  • 0 關(guān)注
  • 2344 瀏覽
慕課專欄
更多

添加回答

舉報(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)