翻譯不完整。請協助 翻譯此英文文件。
let
用於宣告一個,只作用在當前區塊的變數,可以也可以不用給予它一個初始值。
語法
let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];
參數
var1
,var2
, …,varN
- 變數名稱。
value1
,value2
, …,valueN
- 變數的初始值,可以是任何合法的表達式。
描述
let
允許你去宣告只能在目前區塊、階段或表達式中作用的變數,不像 var 關鍵字,它定義了一個全域的變數,或是在整個 function 而不管區塊範圍。
let 的作用區塊
使用 let 在區塊內來定義一個變數。
if (x > y) { let gamma = 12.7 + y; i = gamma * x; }
在使用內部函式時, let
也能夠讓程式碼更加地簡潔。
var list = document.getElementById("list"); for (var i = 1; i <= 5; i++) { var item = document.createElement("LI"); item.appendChild(document.createTextNode("Item " + i)); let j = i; item.onclick = function (ev) { console.log("Item " + j + " is clicked."); }; list.appendChild(item); }
The example above works as intended because the five instances of the (anonymous) inner function refer to five different instances of variable j
. Note that it does not work as intended if you replace let
by var
or if you remove the variable j
and simply use the variable i
in the inner function.
Scoping rules
Variables declared by let
have as their scope the block in which they are defined, as well as in any contained sub-blocks . In this way, let
works very much like var
. The main difference is that the scope of a var
variable is the entire enclosing function:
function varTest() { var x = 31; if (true) { var x = 71; // same variable! console.log(x); // 71 } console.log(x); // 71 } function letTest() { let x = 31; if (true) { let x = 71; // different variable console.log(x); // 71 } console.log(x); // 31 }
在程式裡的最上層,let
和 var
不同,let
並不會在全域物件中建立變數。
舉例來說:
var x = 'global'; let y = 'global'; console.log(this.x); console.log(this.y);
this.x
的輸出結果將會是「global」,但 this.y
則是 undefined
。
Temporal dead zone and errors with let
重複在同區塊內宣告相同名稱的函數,將會造成 SyntaxError
。
if (x) { let foo; let foo; // SyntaxError thrown. }
In ECMAScript 2015, let
will hoist the variable to the top of the block. However, referencing the variable in the block before the variable declaration results in a ReferenceError.
The variable is in a "temporal dead zone" from the start of the block until the declaration is processed.
function do_something() { console.log(foo); // ReferenceError let foo = 2; }
你可能會在 switch
中遇到錯誤,因為所有的 case
都屬於同樣的區塊中。
switch (x) { case 0: let foo; break; case 1: let foo; // SyntaxError for redeclaration. break; }
let 於
for
迴圈的宣告範圍
You can use the let
keyword to bind variables locally in the scope of for
loops. This is different from the var keyword in the head of a for loop, which makes the variables visible in the whole function containing the loop.
var i=0; for ( let i=i ; i < 10 ; i++ ) { console.log(i); }
Scoping rules
for (let expr1; expr2; expr3) statement
In this example, expr2, expr3, and statement are enclosed in an implicit block that contains the block local variables declared by let expr1
. This is demonstrated in the first loop above.
範例
let
vs var
當在區塊中使用,let 限制了變數的作用範圍,注意以下 var 在 function 內外的不同。
var a = 5; var b = 10; if (a === 5) { let a = 4; // The scope is inside the if-block var b = 1; // The scope is inside the function console.log(a); // 4 console.log(b); // 1 } console.log(a); // 5 console.log(b); // 1
迴圈中的 let
You can use the let keyword to bind variables locally in the scope of loops instead of using a global variable (defined using var) for that.
for (let i = 0; i<10; i++) { console.log(i); // 0, 1, 2, 3, 4 ... 9 } console.log(i); // i is not defined
Non-standard let
extensions
let
blocks
let
blocks support has been dropped in Gecko 44 (bug 1167029).
The let
block provides a way to associate values with variables within the scope of a block, without affecting the values of like-named variables outside the block.
Syntax
let (var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]]) block;
Description
The let
block provides local scoping for variables. It works by binding zero or more variables in the lexical scope of a single block of code; otherwise, it is exactly the same as a block statement. Note in particular that the scope of a variable declared inside a let
block using var
is still the same as if it had been declared outside the let
block; such variables still have function scoping. When using the let
block syntax, the parentheses following let
are required. Failure to include them will result in a syntax error.
Example
var x = 5; var y = 0; let (x = x+10, y = 12) { console.log(x+y); // 27 } console.log(x + y); // 5
The rules for the code block are the same as for any other code block in JavaScript. It may have its own local variables established using the let
declarations.
Scoping rules
The scope of variables defined using let
is the let
block itself, as well as any inner blocks contained inside it, unless those blocks define variables by the same names.
let
expressions
let
expression support has been dropped in Gecko 41 (bug 1023609).
The let
expression lets you establish variables scoped only to a single expression.
Syntax
let (var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]]) expression;
Example
You can use let
to establish variables that are scoped only to a single expression:
var a = 5; let(a = 6) console.log(a); // 6 console.log(a); // 5
Scoping rules
Given a let
expression:
let (decls) expr
There is an implicit block created around expr.
規範
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Let and Const Declarations' in that specification. |
Standard | Initial definition. Does not specify let expressions or let blocks. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Let and Const Declarations' in that specification. |
Draft |
瀏覽器相容度
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 41.0 | 12 | 44 (44) | 11 | 17 | ? |
Temporal dead zone | ? | 12 | 35 (35) | 11 | ? | ? |
let expression |
No support | No support | No support | No support | No support | No support |
let block |
No support | No support | No support | No support | No support | No support |
Allowed in sloppy mode | 49.0 |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | ? | 41.0 | 44.0 (44) | ? | ? | ? | 41.0 |
Temporal dead zone | ? | ? | 35.0 (35) | ? | ? | ? | ? |
let expression |
No support | ? | No support | No support | No support | No support | No support |
let block |
No support | ? | No support | No support | No support | No support | No support |
Allow in sloppy mode | No support | 49.0 | 49.0 |
Firefox-specific notes
- Prior to SpiderMonkey 46 (Firefox 46 / Thunderbird 46 / SeaMonkey 2.43), a
TypeError
was thrown on redeclaration instead of aSyntaxError
(bug 1198833). - Prior to SpiderMonkey 44 (Firefox 44 / Thunderbird 44 / SeaMonkey 2.41),
let
was only available to code blocks in HTML wrapped in a<script type="application/javascript;version=1.7">
block (or higher version) and had different semantics. - Support in
Worker
code is hidden behind thedom.workers.latestJSVersion
flag (bug 487070). With version freelet
, this flag is going to be removed in the future (bug 1219523). - ES6 compliance for
let
in SpIderMonkey is tracked in bug 950547