您的位置:首页 > Web前端 > JavaScript

javascript中严格模式中的作用域问题

2016-05-08 15:26 381 查看
通过一个小例子的对比,来看严格模式下与非严格模式中作用域的不同表现。

代码1:

'use strict';
let foo = 'enen';
if(true){
let foo = 'heihei';
console.log(foo); // heihei
}
console.log(foo); // enen


输出结果:

heihei

enen

代码2:

var foo = 'enen';
if(true){
var foo = 'heihei';
console.log(foo); // heihei
}
console.log(foo); // heihei


输出结果:

heihei

heihei

同一个{} 体内,两种不同结果,说明严格模式中块级作用域内的同名变量不受外部影响。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: