typeof语法: typeof([extension])
即typeof后边跟一个表达式,要不要括号都可以。它将返回一个字符串,表示表达式的类型,而表达式的类型只有六种可能:number、string、boolean、object、function、undefined
typeof的误用:
if (x == undefined) if (typeof(x) == undefined) 正确的应该是 if (typeof(x) == “undefined”)
对比这两句话: //var x; if (typeof(x) == “undefined”) alert(“OK1”); if (x==null) alert(“OK2”); 有定义语句var x;时,两句都执行正常。 如果没有定义语句时,第一句没问题,第二句就提示’x’未定义
再对比这两句: x=null; if (typeof(x) == “undefined”) alert(“OK1”); if (x==null) alert(“OK2”);
因此用typeof来检测变量是否有定义是最合适不过的了













![[开源推荐] PHP DFA算法实现敏感词过滤包 php-dfa-sensitive](https://www.viphper.com/wp-content/uploads/2022/11/0f78983556550e6-240x180.png)

