# å˜é‡å£°æ˜Ž

## å˜é‡å£°æ˜Ž

`let`å’Œ`const`æ˜¯JavaScripté‡Œç›¸å¯¹è¾ƒæ–°çš„å˜é‡å£°æ˜Žæ–¹å¼ã€‚ [åƒæˆ‘ä»¬ä¹‹å‰æåˆ°è¿‡çš„](../basic-types#a-note-about-let)ï¼Œ`let`åœ¨å¾ˆå¤šæ–¹é¢ä¸Ž`var`æ˜¯ç›¸ä¼¼çš„ï¼Œä½†æ˜¯å¯ä»¥å¸®åŠ©å¤§å®¶é¿å…åœ¨JavaScripté‡Œå¸¸è§ä¸€äº›é—®é¢˜ã€‚ `const`æ˜¯å¯¹`let`çš„ä¸€ä¸ªå¢žå¼ºï¼Œå®ƒèƒ½é˜»æ­¢å¯¹ä¸€ä¸ªå˜é‡å†æ¬¡èµ‹å€¼ã€‚

å› ä¸ºTypeScriptæ˜¯JavaScriptçš„è¶…é›†ï¼Œæ‰€ä»¥å®ƒæœ¬èº«å°±æ”¯æŒ`let`å’Œ`const`ã€‚ ä¸‹é¢æˆ‘ä»¬ä¼šè¯¦ç»†è¯´æ˜Žè¿™äº›æ–°çš„å£°æ˜Žæ–¹å¼ä»¥åŠä¸ºä»€ä¹ˆæŽ¨èä½¿ç”¨å®ƒä»¬æ¥ä»£æ›¿`var`ã€‚

å¦‚æžœä½ ä¹‹å‰ä½¿ç”¨JavaScriptæ—¶æ²¡æœ‰ç‰¹åˆ«åœ¨æ„ï¼Œé‚£ä¹ˆè¿™èŠ‚å†…å®¹ä¼šå”¤èµ·ä½ çš„å›žå¿†ã€‚ å¦‚æžœä½ å·²ç»å¯¹`var`å£°æ˜Žçš„æ€ªå¼‚ä¹‹å¤„äº†å¦‚æŒ‡æŽŒï¼Œé‚£ä¹ˆä½ å¯ä»¥è½»æ¾åœ°ç•¥è¿‡è¿™èŠ‚ã€‚

## `var` å£°æ˜Ž

ä¸€ç›´ä»¥æ¥æˆ‘ä»¬éƒ½æ˜¯é€šè¿‡`var`å…³é”®å­—å®šä¹‰JavaScriptå˜é‡ã€‚

```typescript
var a = 10;
```

å¤§å®¶éƒ½èƒ½ç†è§£ï¼Œè¿™é‡Œå®šä¹‰äº†ä¸€ä¸ªåä¸º`a`å€¼ä¸º`10`çš„å˜é‡ã€‚

æˆ‘ä»¬ä¹Ÿå¯ä»¥åœ¨å‡½æ•°å†…éƒ¨å®šä¹‰å˜é‡ï¼š

```typescript
function f() {
    var message = "Hello, world!";

    return message;
}
```

å¹¶ä¸”æˆ‘ä»¬ä¹Ÿå¯ä»¥åœ¨å…¶å®ƒå‡½æ•°å†…éƒ¨è®¿é—®ç›¸åŒçš„å˜é‡ã€‚

```typescript
function f() {
    var a = 10;
    return function g() {
        var b = a + 1;
        return b;
    }
}

var g = f();
g(); // returns 11;
```

ä¸Šé¢çš„ä¾‹å­é‡Œï¼Œ`g`å¯ä»¥èŽ·å–åˆ°`f`å‡½æ•°é‡Œå®šä¹‰çš„`a`å˜é‡ã€‚ æ¯å½“`g`è¢«è°ƒç”¨æ—¶ï¼Œå®ƒéƒ½å¯ä»¥è®¿é—®åˆ°`f`é‡Œçš„`a`å˜é‡ã€‚ å³ä½¿å½“`g`åœ¨`f`å·²ç»æ‰§è¡Œå®ŒåŽæ‰è¢«è°ƒç”¨ï¼Œå®ƒä»ç„¶å¯ä»¥è®¿é—®åŠä¿®æ”¹`a`ã€‚

```typescript
function f() {
    var a = 1;

    a = 2;
    var b = g();
    a = 3;

    return b;

    function g() {
        return a;
    }
}

f(); // returns 2
```

### ä½œç”¨åŸŸè§„åˆ™

å¯¹äºŽç†Ÿæ‚‰å…¶å®ƒè¯­è¨€çš„äººæ¥è¯´ï¼Œ`var`å£°æ˜Žæœ‰äº›å¥‡æ€ªçš„ä½œç”¨åŸŸè§„åˆ™ã€‚ çœ‹ä¸‹é¢çš„ä¾‹å­ï¼š

```typescript
function f(shouldInitialize: boolean) {
    if (shouldInitialize) {
        var x = 10;
    }

    return x;
}

f(true);  // returns '10'
f(false); // returns 'undefined'
```

æœ‰äº›è¯»è€…å¯èƒ½è¦å¤šçœ‹å‡ éè¿™ä¸ªä¾‹å­ã€‚ å˜é‡`x`æ˜¯å®šä¹‰&#x5728;_`if`è¯­å¥é‡Œé¢_ï¼Œä½†æ˜¯æˆ‘ä»¬å´å¯ä»¥åœ¨è¯­å¥çš„å¤–é¢è®¿é—®å®ƒã€‚ è¿™æ˜¯å› ä¸º`var`å£°æ˜Žå¯ä»¥åœ¨åŒ…å«å®ƒçš„å‡½æ•°ï¼Œæ¨¡å—ï¼Œå‘½åç©ºé—´æˆ–å…¨å±€ä½œç”¨åŸŸå†…éƒ¨ä»»ä½•ä½ç½®è¢«è®¿é—®ï¼ˆæˆ‘ä»¬åŽé¢ä¼šè¯¦ç»†ä»‹ç»ï¼‰ï¼ŒåŒ…å«å®ƒçš„ä»£ç å—å¯¹æ­¤æ²¡æœ‰ä»€ä¹ˆå½±å“ã€‚ æœ‰äº›äººç§°æ­¤&#x4E3A;_`var`ä½œç”¨&#x57DF;_&#x6216;_å‡½æ•°ä½œç”¨åŸŸ_ã€‚ å‡½æ•°å‚æ•°ä¹Ÿä½¿ç”¨å‡½æ•°ä½œç”¨åŸŸã€‚

è¿™äº›ä½œç”¨åŸŸè§„åˆ™å¯èƒ½ä¼šå¼•å‘ä¸€äº›é”™è¯¯ã€‚ å…¶ä¸­ä¹‹ä¸€å°±æ˜¯ï¼Œå¤šæ¬¡å£°æ˜ŽåŒä¸€ä¸ªå˜é‡å¹¶ä¸ä¼šæŠ¥é”™ï¼š

```typescript
function sumMatrix(matrix: number[][]) {
    var sum = 0;
    for (var i = 0; i < matrix.length; i++) {
        var currentRow = matrix[i];
        for (var i = 0; i < currentRow.length; i++) {
            sum += currentRow[i];
        }
    }

    return sum;
}
```

è¿™é‡Œå¾ˆå®¹æ˜“çœ‹å‡ºä¸€äº›é—®é¢˜ï¼Œé‡Œå±‚çš„`for`å¾ªçŽ¯ä¼šè¦†ç›–å˜é‡`i`ï¼Œå› ä¸ºæ‰€æœ‰`i`éƒ½å¼•ç”¨ç›¸åŒçš„å‡½æ•°ä½œç”¨åŸŸå†…çš„å˜é‡ã€‚ æœ‰ç»éªŒçš„å¼€å‘è€…ä»¬å¾ˆæ¸…æ¥šï¼Œè¿™äº›é—®é¢˜å¯èƒ½åœ¨ä»£ç å®¡æŸ¥æ—¶æ¼æŽ‰ï¼Œå¼•å‘æ— ç©·çš„éº»çƒ¦ã€‚

### æ•èŽ·å˜é‡æ€ªå¼‚ä¹‹å¤„

å¿«é€Ÿçš„çŒœä¸€ä¸‹ä¸‹é¢çš„ä»£ç ä¼šè¿”å›žä»€ä¹ˆï¼š

```typescript
for (var i = 0; i < 10; i++) {
    setTimeout(function() { console.log(i); }, 100 * i);
}
```

ä»‹ç»ä¸€ä¸‹ï¼Œ`setTimeout`ä¼šåœ¨è‹¥å¹²æ¯«ç§’çš„å»¶æ—¶åŽæ‰§è¡Œä¸€ä¸ªå‡½æ•°ï¼ˆç­‰å¾…å…¶å®ƒä»£ç æ‰§è¡Œå®Œæ¯•ï¼‰ã€‚

å¥½å§ï¼Œçœ‹ä¸€ä¸‹ç»“æžœï¼š

```
10
10
10
10
10
10
10
10
10
10
```

å¾ˆå¤šJavaScriptç¨‹åºå‘˜å¯¹è¿™ç§è¡Œä¸ºå·²ç»å¾ˆç†Ÿæ‚‰äº†ï¼Œä½†å¦‚æžœä½ å¾ˆä¸è§£ï¼Œä½ å¹¶ä¸æ˜¯ä¸€ä¸ªäººã€‚ å¤§å¤šæ•°äººæœŸæœ›è¾“å‡ºç»“æžœæ˜¯è¿™æ ·ï¼š

```
0
1
2
3
4
5
6
7
8
9
```

è¿˜è®°å¾—æˆ‘ä»¬ä¸Šé¢æåˆ°çš„æ•èŽ·å˜é‡å—ï¼Ÿ æˆ‘ä»¬ä¼ ç»™`setTimeout`çš„æ¯ä¸€ä¸ªå‡½æ•°è¡¨è¾¾å¼å®žé™…ä¸Šéƒ½å¼•ç”¨äº†ç›¸åŒä½œç”¨åŸŸé‡Œçš„åŒä¸€ä¸ª`i`ã€‚

è®©æˆ‘ä»¬èŠ±ç‚¹æ—¶é—´æ€è€ƒä¸€ä¸‹è¿™æ˜¯ä¸ºä»€ä¹ˆã€‚ `setTimeout`åœ¨è‹¥å¹²æ¯«ç§’åŽæ‰§è¡Œä¸€ä¸ªå‡½æ•°ï¼Œå¹¶ä¸”æ˜¯åœ¨`for`å¾ªçŽ¯ç»“æŸåŽã€‚ `for`å¾ªçŽ¯ç»“æŸåŽï¼Œ`i`çš„å€¼ä¸º`10`ã€‚ æ‰€ä»¥å½“å‡½æ•°è¢«è°ƒç”¨çš„æ—¶å€™ï¼Œå®ƒä¼šæ‰“å°å‡º`10`ï¼

ä¸