当我执行以下脚本时,警告语句正在打印函数为什么会这样? 执行上下文中会发生什么?为什么变量basicPattern的未定义值不打印?
function basicPattern(){
var o = 5;
return o;
}
var basicPattern;
console.log(basicPattern);
答案 0 :(得分:2)
function basicPattern(){
var o = 5;
return o;
}
var basicPattern;
console.log(basicPattern);
评估与此相同(IE错误被忽视):
var basicPattern;
basicPattern = function basicPattern(){
var o = 5;
return o;
};
console.log(basicPattern);
由于basicPattern
已经声明,因此再次声明它将不会产生任何影响,因为声明
被吊起并合并。如果你已经转让给5
,那就是这样的:
var basicPattern;
basicPattern = function basicPattern(){
var o = 5;
return o;
};
basicPattern = 5;
console.log(basicPattern);
详细了解吊装:http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting