当前位置:网站首页>ES6 uses the const command to declare variables

ES6 uses the const command to declare variables

2020-12-08 10:23:27 Do you know

In this section, we learn ES6 Medium const command ,const It's also ES6 A new command is added in .const Declare a read-only constant , Once a constant is declared , It must be initialized immediately . If you change the value of a constant or just declare it without assigning an initial value, an error will be reported .

const Statement

const Declare a read-only constant , Once declared , You can't change the value of a constant . actually , It's not the value that can't be changed , Instead, the memory address that the variable points to holds the same data .

Example :

Use const The command declares that the variable is actually the same as let You can use it in the same way , For example, declare a constant a

const a = 1;
console.log(a);

Constants must be initialized after declaration

const After a constant is declared, it must be initialized immediately , It cannot be left for later assignment .

Example :
const a;
a = 1;
console.log(a);

Error message :

SyntaxError: Missing initializer in const declaration

The wrong message tells us ,const Missing initializer in declaration , In other words, there is no initial value assigned .

Constant cannot be changed after being declared

Use const After the command declares a constant , The value of a constant cannot be changed , Otherwise, an error will be reported .

Example :

For example, a statement a And assign it as 1 after , modify a The value of is 10:

const a = 1;
a = 10; //  The value of a constant cannot be changed 
console.log(a);

The error message is as follows :

TypeError: Assignment to constant variable.

const Other features

const and let There are still many similarities , For example, only valid within the block level scope where the declaration is located .

Example :
{
    const a = 1;
}
console.log(a);

The error information is as follows , Prompt us a Not declared :

ReferenceError: a is not defined

besides , also const The constants declared by the command are not promoted , There is also a temporary dead zone , Can only be used after the declared position .

Example :
if(true){
    console.log(PI); 
    const PI = 10;
}

The error information is as follows , You cannot call constants before initialization PI

ReferenceError: Cannot access 'PI' before initialization

const Declared constant , Also with the let Same non repeatable statement .

Example :
var a = 10;
let b = 20;

//  The following two lines will be wrong 
const a = 100;
const b = 200;

The error message is as follows :

SyntaxError: Identifier 'a' has already been declared

const The essence

const Actually guaranteed , It's not that the value of a variable can't be changed , Instead, the memory address that the variable points to holds the same data .

  • For simple types of data , For example, numerical value 、 character string 、 Boolean value , The value is stored at the memory address that the variable points to , So it's equivalent to a constant .
  • But for data of composite type , For example, objects and arrays , The memory address that the variable points to , It's just a pointer to the actual data ,const It can only be guaranteed that the pointer is fixed ( Always point to another fixed address ), As for whether the data structure it points to is variable , It's totally out of control .
Example :

Let's look at the following example :

const xkd = {};

xkd.prop = 123;
console.log(xkd.prop);  //  Output :123


xkd = {};  //  Output :TypeError: Assignment to constant variable.

In the above code , Constant xkd It stores an address , This address points to an object . It's just this address that's immutable , That can't leave xkd Point to another address , But the object itself is variable , So you can still add new properties to it .

If you want to see more, you can see :https://www.9xkd.com/

版权声明
本文为[Do you know]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201208102259311b.html