<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script src="js/browser.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> 'use strict'; //const用来声明常量 const PI = 3.1415926; //const PI = 3; //报错 //var PI; //报错 //const foo; //必须立即初始化 //const只对声明的块级作用域内有效 if(true){ const MAX = 6; } //alert(MAX); //MAX is not defined var message = "hello"; var age = 25; //不能重复声明变量 //const message = "aaa"; //报错 //let age = 26; //报错 //如果是对象或者数组,只能保证地址不变,但是变量的值可以变 const foo = {}; foo.prop = 123; console.log(foo.prop); //123 //foo = {}//地址改变了,所以报错了 //如果真的想冻结一个对象,可以使用Object.freeze const foo2 = Object.freeze({}); //foo2.prop = 123; //报错 //对象彻底冻结的方法,即属性也冻结 var constantize = (obj) =>{ Object.freeze(obj); Object.keys(obj).forEach(key , value) =>{ if(typeof obj[key] === 'object'){ constantize(obj[key]); } } } //没理解这上面写的啥玩意 </script> </body> </html>