const
一旦声明,值不可变
1 | const PI = 3.14 |
只声明不赋值也会报错。
1 | const foo; //SyntaxError: Missing initializer in const declaration |
实质为变量指向的内存地址不可变动
const 只能保证这个指针是固定的,不能控制数据结构的变化。
1 | const foo = {} |
对象冻结方法
使用 Object.freeze 函数,冻结对象
1 | const foo = Object.freeze({}) |
冻结属性的函数
1 | var makeConstant = (obj) => { |