笔试题:
var x = 1;
if (function () {}) {
x += typeof f;
}
console.log(x);
1undefined
解析:
//if (function f() {}) -->function f() {}作为表达式,function f消失,
// 所以在if里面,f为未声明
//任何一个变量,未经声明使用都会报错。
//!但是,typeof其时,不会报错,而是undefined,typeof是唯一不报错的操作符
console.log(typeof f);
console.log(typeof (typeof f));
console.log(1 + undefined);
undefined
string
NaN
另外,使用一个对象没有的属性,控制台显示undefined,而不是像没声明的变量一样报错error。
易记错的类型转换:
//alert(typeof (undefined));// ->undefined
//alert(typeof (NaN));//->number
//alert(typeof (null));//->object
var a = "123abc";
//alert(typeof (+a));/*->NaN->numnber*/
//alert(typeof (!!a));//->boolean/*判断运算符的结果*/
//alert("11" + 11);//->1111
//alert(typeof (typeof (a)));//->string