ES6新增語法--const

const(★★★)

聲明常量,常量就是值(內存地址)不能變化的量atom

具備塊級做用域

 if (true) { 
    const a = 10;
}
console.log(a) // a is not defined

聲明常量時必須賦值

const PI; // Missing initializer in const declaration

常量賦值後,值不能修改

const PI = 3.14;
PI = 100; // Assignment to constant variable.

const ary = [100, 200];
ary[0] = 'a';
ary[1] = 'b';
console.log(ary); // ['a', 'b'];
ary = ['a', 'b']; // Assignment to constant variable.

小結

  • const聲明的變量是一個常量spa

  • 既然是常量不能從新進行賦值,若是是基本數據類型,不能更改值,若是是複雜數據類型,不能更改地址值內存

  • 聲明 const時候必需要給定值作用域

相關文章
相關標籤/搜索