[JavaScript Interview] Can a constant declared with const be modified?

The difference between var, const, let?

  1. Var has variable promotion, const and let cannot
  2. let and const have block-level scope, var does not
  3. var allows repeated declarations, let and const do not allow repeated declarations of variables in the same scope
  4. var and let can modify the declared variable, const declares a read-only constant, once declared, the value of the constant cannot be changed, but for reference types such as objects and arrays, the memory address cannot be modified, and the value inside can be modified

Objects declared with const can be modified?

const obj  = {
    
    
    name: 'chen'
}
console.log(obj.name);
obj.name = 'jiu'
console.log(obj.name)

insert image description here
The const declaration only fixes the value of obj, but the content inside the value (the object) is not fixed. Only when we want to change the value of the entire obj, const still throws an error.

 const test = {
    
    
            value: 1
        }
        test = {
    
    
            name: 'test2'
        }
        console.log(test)

insert image description here
What const actually guarantees 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. The variables of the reference data type are stored in the 'stack memory', the value is stored in the 'heap memory', and the pointer points to the corresponding value in the 'heap memory'. Therefore, the reference data type defined by const cannot be changed. pointer', so the value can be modified through the property.

If we want to freeze the object, we can use the following method:

Object.freeze

const objTest = Object.freeze({
    
    })
console.log(objTest)
objTest.name = 'd'
console.log(objTest)

insert image description here

Object.defineProperty()

       const objTest = {
    
    
            "property1": 123
        };

        Object.defineProperty(objTest, 'property1', {
    
    
            writable: false // 设置不允许写入
        });

        objTest.property1 = 888;
        console.log(objTest);

insert image description here

Object.seal()

You can prevent adding attributes, removing attributes, and cannot prevent worth modifying

const object1 = {
    
    
  property1: 123
};

Object.seal(object1);
object1.property1 = 456;
// object1.property1 // 123

delete object1.property1; // cannot delete when sealed
// object1.property1 // 123

Guess you like

Origin blog.csdn.net/qq_40992225/article/details/127624457