var name='World!';
(function(){
if(typeof name ==='undefined'){
var name = 'Jayee'
console.log("1"+name);
}else{
console.log("2"+name)
}
})();
答案与解析在下面:
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
答案:
解析:
var是函数作用域,不是块级作用域
var name='World!';
(function(){
//var name; //3.变量提升在此隐式声明,所以name此时=undefined
if(typeof name ==='undefined'){//1.先在本函数作用域找变量
//4.由于name=undefined,所以这个判断条件成立
var name = 'Jayee'//2.变量提升,隐式声明在函数开始时
console.log("1"+name);//5.执行此语句
}else{
console.log("2"+name)
}
})();