ES6中的const命令【转】

1.const声明一个只读常量,一旦声明,常量的值就不能改变html

1 const PI=3.1415;
2 console.log(PI);//3.1415
3 
4 PI=3;//Uncaught TypeError: Assignment to constant variable.

2.const一旦声明常量,就必须当即初始化,不能留到之后赋值post

1 const WIDTH;//Uncaught SyntaxError: Missing initializer in const declaration

3.const声明的常量只在当前做用域内有效spa

1 if(true){
2   const NAME='XG'
3 }
4 
5 console.log(NAME);//Uncaught ReferenceError: NAME is not defined

4.const声明的常量不存在“声明提早”,只能先声明后使用code

1 if(true){
2   console.log(NAME);//Uncaught ReferenceError: NAME is not defined
3   const NAME='XG';
4 }

5.const不可重复声明htm

6.const声明的常量若是保存的是引用类型的数据,只会保证该数据的地址不变,并不能保证该数据不变blog

转载自:http://www.cnblogs.com/xgblogs/p/6142792.html作用域

相关文章
相关标签/搜索