ES6基础之——恒量const

使用const可以去声明一个衡量,这样就不能够给这个衡量去重新分配一个新的值

 

例子:
const fruit ="apple";
console.log(fruit);

const fruit ="lemon";
console.log(fruit);

 

在控制台上会报语法错误:
Uncaught SyntaxError: Identifier 'fruit' has already benn declared at <anonymous>:1:1

 

注意:const限制的是给const分配值的动作,并不是限制衡量里面的值
 
例子:
const fruit =[];
fruit.push('apple');
fruit.push('lemon');
console.log(fruit); //[apple,lemon]

 

如果重新给fruit分配一个值:
fruit=[];
console.log(fruit); //Uncaught SyntaxError: Assignment to constant variable.

 

这就是因为我们重新分配了fruit的值
posted @ 2019-06-10 19:04  qnlz  阅读(247)  评论(0编辑  收藏  举报