const command in ES6

const basic usage

1. const declares a read-only constant. Once declared, the constant's value cannot be changed.

const PI = 3.1415;
PI // 3.1415
PI = 3;
// TypeError: Assignment to constant variable.

Variables declared by const must not change their value, which means that once a variable is declared by const, it must be initialized immediately and cannot be left for later assignment.

const foo;
// SyntaxError: Missing initializer in const declaration

2. The scope of const is the same as the let command: it is only valid in the block-level scope where the declaration is located.

if (true) {
    
    
  const MAX = 5;
}
MAX // Uncaught ReferenceError: MAX is not defined

3. The constant declared by the const command is also not promoted , and there is also a temporary dead zone , which can only be used after the declared position.

if (true) {
    
    
  console.log(MAX); // ReferenceError
  const MAX = 5;
}

4. Constants declared by const cannot be declared repeatedly like let .

var message = "Hello!";
let age = 25;
// 以下两行都会报错
const message = "Goodbye!";
const age = 30;

Nature

constIn fact, what is guaranteed is not that the value of the variable cannot be changed, but that the data stored in the memory address pointed to by the variable cannot be changed.
For simple types of data (numbers, strings, Boolean values), the value is stored at the memory address pointed to by the variable, so it is equivalent to a constant.
But for composite types of data (mainly objects and arrays), the memory address pointed to by the variable is only a pointer to the actual data, and const can only guarantee that the pointer is fixed (that is, it always points to another fixed address) , As for whether the data structure it points to is mutable, it is completely out of control. Therefore, one must be very careful about declaring an object as a constant.

const foo = {
    
    };
// 为 foo 添加一个属性,可以成功
foo.prop = 123;
foo.prop // 123
// 将 foo 指向另一个对象,就会报错
foo = {
    
    }; // TypeError: "foo" is read-only

In the above code, the constant foo stores an address, which points to an object. Only this address is immutable, that is, foo cannot be pointed to another address, but the object itself is mutable, so new properties can still be added to it.

const a = [];
a.push('Hello'); // 可执行
a.length = 0;    // 可执行
a = ['Dave'];    // 报错

If you really want to freeze the object, you should use the Object.freeze method.

const foo = Object.freeze({
    
    });
// 常规模式时,下面一行不起作用;
// 严格模式时,该行会报错
foo.prop = 123;

In the above code, the constant foo points to a frozen object, so adding new properties does not work, and an error will be reported in strict mode.

In addition to freezing the object itself, the properties of the object should also be frozen. Below is a function that completely freezes an object.

var constantize = (obj) => {
    
    
  Object.freeze(obj);
  Object.keys(obj).forEach( (key, i) => {
    
    
    if ( typeof obj[key] === 'object' ) {
    
    
      constantize( obj[key] );
    }
  });
};

Six ways to declare variables in ES6

ES5 has only two ways to declare variables: the var command and the function command.
In addition to adding let and const commands in ES6, there are two other ways to declare variables: import commands and class commands. So, ES6 has a total of 6 ways to declare variables.

Comparable to the const command in ES6

おすすめ

転載: blog.csdn.net/qq_38970408/article/details/122365719