1.const用來定義常量,賦值之后不能再賦值,再次賦值會報錯。
<script>
//1.定義常量,賦值后不能再賦值,在賦值報錯
const count = 1
// count = 2
</script>
2.const不能只聲明不賦值,會報錯。
<script>
//2.只聲明不賦值,必須賦值
// const count;
</script>
3.const常量含義是你不能改變其指向的對象,例如user,都是你可以改變user屬性。
<script>
//3.常量的含義是你不能改變其指向的對象user,但是你可以改變user屬性
const user = {
name:"zzz",
age:24,
height:175
}
console.log(user)
user.name = "ttt"
user.age = 22
user.height = 188
console.log(user)
</script>
const內存地址詳解
對象count一開始只是0x10的地址,直接將count(給count重新賦值,指向一個新的對象)指向地址改為0x20會報錯,const是常量,無法更改對象地址。
對象user一開始指向0x10地址,user有Name
、Age
、Height
三個屬性,此時修改屬性Name='ttt'
,user對象的地址未改變,不會報錯。