2 回答
![?](http://img1.sycdn.imooc.com/545865b000016a9202200220-100-100.jpg)
TA贡献1909条经验 获得超7个赞
您会看到这一点,因为typeof运算符将值"undefined"作为string返回给您。
来自 MDN文档:
typeof 运算符返回一个字符串,指示未计算的操作数的类型。
您可以typeof()在上面执行 atypeof(typeof(oneT))来检查它是否确实向您返回了一个字符串。
在f2()获取调用,但你看不到任何输出作为if块被完全忽略,因为你是一个比较字符串"undefined"从返回typeof(oneT)与undefined价值:
function f1() {
var oneT = 55; //function scope
console.log(oneT);
}
f1();
console.log(typeof(typeof(oneT))); //string
function f2() {
if (typeof(oneT) == undefined) { //false and if is skipped
console.log("oneT can't be read outside the f1() as it's scope is limited to the fn f1().");
}
console.log("Inside f2"); //prints this
}
f2();
function f3() {
if (typeof(oneT) === "undefined") { //true
console.log("oneT can't be read outside the f1() as it's scope is limited to the fn f1().");
}
}
f3();
添加回答
举报