javascript 常量
JavaScript常數 (JavaScript Constants)
Before ES15, the only way to declare variables using the var keyword. JavaScript's inbuilt feature of hoisting variables could be carried out using the var keyword. If you're unfamiliar with variables in JavaScript, have a glance at Variables in JavaScript article on the website. If you wish to know what hoisting is, read through Hoisting in JavaScript.
在ES15之前,這是使用var關鍵字聲明變量的唯一方法。 JavaScript的內置變量提升功能可以使用var關鍵字執行。 如果您不熟悉JavaScript中的變量,請瀏覽網站上的JavaScript中的變量文章。 如果您想知道什么是提升,請通讀JavaScript中的提升 。
Today we'll look at constants. Variables after ES15 could be declared in two ways - let and const. Before we dive deeper into const, let's understand what a constant is?
今天我們來看一下常量 。 ES15之后的變量可以兩種方式聲明: let和const 。 在深入研究const之前,讓我們了解一個常數是什么?
Constants in most languages are something that retains their value during their block or wherever their life persists. In JavaScript, constants are values that we cannot modify later directly. This means if we declare a constant with a certain value, assigning some other value later in the program would result in an error.
大多數語言中的常量在其阻塞期間或生命持續存在的地方都可以保留其價值。 在JavaScript中,常量是我們以后無法直接修改的值。 這意味著,如果我們聲明一個具有特定值的常數,則稍后在程序中分配其他一些值將導致錯誤。
const a=10;
a=25;
Output
輸出量
Uncaught TypeError: Assignment to constant variable. at <anonymous>:1:2
We get a TypeError.
我們得到一個TypeError 。
We declare a constant using the keyword const.
我們使用關鍵字const聲明一個常量。
const follows block scope. Consider the following,
const遵循塊作用域。 考慮以下,
var t=10; //Value of t here is 10
{
const t=12; //Value of t here is 12
}
//Value of t here is 10
Also, constants declared using the const keyword must be initialized and declared at the same time.
另外, 使用const關鍵字聲明的常量必須同時初始化和聲明 。
const g=9.8;
//is correct, but
const g;
g=9.8i
//is incorrect.
However, an important catch to note that the const keyword is not what it looks like. The more accurate statement regarding const would be that it defines a constant reference to a value. This means that primitive values assigned to a variable using the const keyword cannot be altered but with objects, we are bound to no such restriction.
但是,需要注意的重要一點是const關鍵字不是它的外觀。 關于const的更準確的陳述是,它定義了對值的常量引用 。 這意味著使用const關鍵字分配給變量的原始值不能更改,但是對于對象,我們不受任何限制。
If we declare an object with the const keyword, we can later change it by giving it new properties.
如果我們使用const關鍵字聲明一個對象,我們以后可以通過為其賦予新屬性來對其進行更改。
const person={
Name: 'Fuzzy Sid',
Age: 20
}
person.gender='Male';
Adding a new property to a constant object does not yield us an error.
向常量對象添加新屬性不會產生錯誤。
Similarly, we can change the elements of an array defined using the const keyword.
同樣, 我們可以更改使用const關鍵字定義的數組的元素 。
const arr=[1,2,3,4];
console.log(arr); //[1,2,3,4]
arr[0]=0;
console.log(arr); //[0,2,3,4]
arr.push_back(5);
console.log(arr); //[0,2,3,4,5]
However, we cannot reassign new values to the array.
但是, 我們無法將新值重新分配給array 。
arr = [9,8,7,6]
Output
輸出量
Uncaught TypeError: Assignment to constant variable.at <anonymous>:1:4
Would give an error. But the trick around this would be to individually change the values of the array or maybe run a loop for the same.
會給出一個錯誤。 但是,解決這個問題的技巧是單獨更改數組的值,或者為該數組運行一個循環。
for(let i=0,cnt=9; i<arr.length; i++,cnt--){
arr[i]=cnt;
}
Remember that const variables are not supported in versions of Internet Explorer before 10 and they are not hoisted. So you cannot use them before the declaration.
請記住,Internet Explorer 10之前的版本不支持const變量,也不會使用它們。 因此,您不能在聲明之前使用它們。
翻譯自: https://www.includehelp.com/code-snippets/constants-in-javascript.aspx
javascript 常量