我们都知道const 是声明常量的,如果用const声明过后的变量是不可以修改的
const id = 1
id = 2
console.log(id) // Uncaught TypeError: Assignment to constant variable
但如果是const 声明了对象,其实对象的属性是可以修改的,对象属性被赋值为常量是不受保护的
const obj = {
name:'王大锤'
}
obj.name = '李大锤'
console.log(obj.name) // '李大锤'
不仅如此,如果是const声明了数组,数组里面的值也是可以修改的,数组的被定义为常量也是不受保护的
const arr = [1,2,3,4,5]
arr[0] = '二蛋'
console.log(arr) // ['二蛋',2,3,4,5]