

javascript 严格模式
第一次接触let关键字,有一个要非常非常要注意的概念就是”javascript 严格模式”,比如下述的代码运行就会报错:
let hello = 'hello world.'; console.log(hello);
错误信息如下:
let hello = 'hello world.'; ^^^ SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode ...
解决方法就是,在文件头添加”javascript 严格模式”声明:
'use strict'; let hello = 'hello world.'; console.log(hello);
let和var关键字的异同
声明后未赋值,表现相同
'use strict';
(function() {
var varTest;
let letTest;
console.log(varTest); //使用未声明的变量,表现不同:
(function() {
console.log(varTest); //重复声明同一个变量时,表现不同:
'use strict';
(function() {
var varTest = 'test var OK.';
let letTest = 'test let OK.';
var varTest = 'varTest changed.';
let letTest = 'letTest changed.'; //直接报错:SyntaxError: Identifier 'letTest' has already been declared
console.log(varTest); //变量作用范围,表现不同
'use strict';
(function() {
var varTest = 'test var OK.';
let letTest = 'test let OK.';
{
var varTest = 'varTest changed.';
let letTest = 'letTest changed.';
}
console.log(varTest); //