JavaScript-Array-Const
- Vorherige Seite JS-Array-Iteration
- Nächste Seite JS-Daten
ECMAScript 2015 (ES6)
Im Jahr 2015 führte JavaScript einen wichtigen neuen Schlüsselwort ein:const
.
使用 const
Die Deklaration von Arrays ist ein gängiges Vorgehen:
Beispiel
const cars = ["Saab", "Volvo", "BMW"];
Kann nicht neu zugewiesen werden
用 const
Deklarierte Arrays können nicht neu zugewiesen werden:
Beispiel
const cars = ["Saab", "Volvo", "BMW"]; cars = ["Toyota", "Volvo", "Audi"]; // FALSCH
Das Array ist keine Konstante
Schlüsselwort const
Es ist irreführend.
Es definiert keine Konstantenarrays. Es definiert eine konstante Referenz auf das Array.
Daher können wir die Elemente von Konstantenarrays immer noch ändern.
Elemente können neu zugewiesen werden
Sie können Elemente von Konstantenarrays ändern:
Beispiel
// Sie können Konstantenarrays erstellen: const cars = ["Saab", "Volvo", "BMW"]; // Sie können Elemente ändern: cars[0] = "Toyota"; // Sie können Elemente hinzufügen: cars.push("Audi");
Browser Support
Internet Explorer 10 或更早的版本不支持 const
关键字。
下表注明了完全支持 const 关键字的首个浏览器版本:
Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 49 | IE 11 / Edge | Firefox 36 | Safari 10 | Opera 36 |
2016 年 3 月 | 2013 年 10 月 | 2015 年 2 月 | 2016 年 9 月 | 2016 年 3 月 |
声明时赋值
JavaScript const
变量在声明时必须赋值:
意思是:用 const
声明的数组必须在声明时进行初始化。
使用 const
而不初始化数组是一个语法错误:
Beispiel
不起作用:
const cars; cars = ["Saab", "Volvo", "BMW"];
用 var
声明的数组可以随时初始化。
您甚至可以在声明之前使用该数组:
Beispiel
没问题:
cars = ["Saab", "Volvo", "BMW"]; var cars;
const 块作用域
用 const
声明的数组具有块作用域.
在块中声明的数组与在块外声明的数组不同:
Beispiel
const cars = ["Saab", "Volvo", "BMW"]; // 此处 cars[0] 为 "Saab" { const cars = ["Toyota", "Volvo", "BMW"]; // 此处 cars[0] 为 "Toyota" } // 此处 cars[0] 为 "Saab"
用 var
声明的数组没有块作用域:
Beispiel
var cars = ["Saab", "Volvo", "BMW"]; // 此处 cars[0] 为 "Saab" { var cars = ["Toyota", "Volvo", "BMW"]; // 此处 cars[0] 为 "Toyota" } // 此处 cars[0] 为 "Toyota"
您可以在以下章节中学习更多关于块作用域的内容:JavaScript-Bereich.
重新声明数组
在程序中的任何位置都允许用 var
Erneute Deklaration des Arrays:
Beispiel
var cars = ["Volvo", "BMW"]; // 允许 var cars = ["Toyota", "BMW"]; // 允许 cars = ["Volvo", "Saab"]; // 允许
不允许在同一作用域或同一块中将数组重新声明或重新赋值给 const
:
Beispiel
var cars = ["Volvo", "BMW"]; // 允许 const cars = ["Volvo", "BMW"]; // 不允许 { var cars = ["Volvo", "BMW"]; // 允许 const cars = ["Volvo", "BMW"]; // 不允许 }
不允许在同一作用域或同一块中重新声明或重新赋值现有的 const
数组:
Beispiel
const cars = ["Volvo", "BMW"]; // Erlaubt const cars = ["Volvo", "BMW"]; // 不允许 var cars = ["Volvo", "BMW"]; // Nicht erlaubt cars = ["Volvo", "BMW"]; // Nicht erlaubt { const cars = ["Volvo", "BMW"]; // Erlaubt const cars = ["Volvo", "BMW"]; // Nicht erlaubt var cars = ["Volvo", "BMW"]; // Nicht erlaubt cars = ["Volvo", "BMW"]; // Nicht erlaubt }
Erlaubt, ein Array in einem anderen Bereich oder einem anderen Block zu verwenden const
Erneute Deklaration des Arrays:
Beispiel
const cars = ["Volvo", "BMW"]; // Erlaubt { const cars = ["Volvo", "BMW"]; // Erlaubt } { const cars = ["Volvo", "BMW"]; // Erlaubt }
Vollständiges Array-Referenzhandbuch
Für eine vollständige Referenz besuchen Sie bitte unsere vollständige JavaScript-Array-Referenzhandbuch.
Dieses Handbuch enthält alle Beschreibungen und Beispiele der Array-Attribute und -Methoden.
- Vorherige Seite JS-Array-Iteration
- Nächste Seite JS-Daten