Es6 series --const

const is used to define a constant, once declared, its value cannot be changed

A constant declared by const must not change the value. This means that once a const is declared a constant, it must be initialized immediately and cannot be left until the final assignment.

 

 The scope of const is the same as that of let: only valid in the block-level scope where the declaration is located

Const variables are also not promoted, and there is also a temporary dead zone, which can only be used after declaration.

Like let, const cannot repeat variable declarations.

 

For variables of composite types, the variable name does not point to the data, but to the address where the data is located. The const command only ensures that the address pointed to by the variable name remains unchanged, and does not guarantee that the data at the address remains unchanged, so you must be very careful when declaring an object as a constant

it means that

There is no problem with writing this way, but you will make mistakes if you write it this way

The above code constant foo stores an address that points to an object. The only thing immutable is this address, you can't point foo to another address but the object itself is mutable, so you can still add new properties to it.

Here is another example

In the above code, the constant a is an array, and the array itself is writable, but if you assign another array to a, an error will be reported

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325005988&siteId=291194637