在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ esLint里no-case-declarations的意義在哪

esLint里no-case-declarations的意義在哪

官網(wǎng)的解釋:The reason is that the lexical declaration is visible in the entire switch block but it only gets initialized when it is assigned, which will only happen if the case where it is defined is reached.
沒怎么看懂

回答
編輯回答
撿肥皂

有中文網(wǎng)站 no-case-declarations

該規(guī)則禁止詞法聲明 (let、const、functionclass) 出現(xiàn)在 casedefault 子句中。原因是,詞法聲明在整個(gè) switch 語句塊中是可見的,但是它只有在運(yùn)行到它定義的 case 語句時(shí),才會(huì)進(jìn)行初始化操作。

為了保證詞法聲明語句只在當(dāng)前 case 語句中有效,將你子句包裹在塊中。

該規(guī)則旨在避免訪問未經(jīng)初始化的詞法綁定以及跨 case 語句訪問被提升的函數(shù)。

switch (foo) {
    case 1:
        let x = 1;
        break;
    case 2:
        const y = 2;
        break;
    case 3:
        function f() {}
        break;
    default:
        class C {}
}

大概是指上面case 1里的xcase 2里也會(huì)生效,所以要用{}包起來,防止x提升到整個(gè)switch語句。

2017年10月24日 01:18