标签:
typeof操作符:
(1) 检测给定变量的数据类型;
(2) 对一个值使用typeof操作符可能返回下列某个字符串:
"undefined"——如果这个值未定义;
"boolean"——如果这个值是布尔值;
"string"——如果这个值是字符串;
"number"——如果这个值是数值;
"object"——如果这个值是对象或null;
"function"——如果这个值是函数;
var person=new Object();
function sayHello(){console.log("hello");}
console.log(typeof(a)); //undefined
console.log(typeof(true)); //boolean
console.log(typeof("123")); //string
console.log(typeof(123)); //number
console.log(typeof(person)); //object
console.log(typeof(sayHello)); //function
(3) typeof是一个操作符而不是函数,因此圆括号尽管可以使用,但不是必需的。
(4) typeof null会返回"object",因为特殊值null被认为是一个空的对象引用。
(5) 浏览器差异:Safari5及之前版本、Chrome7及之前版本在对正则表达式调用typeof操作符时会返回"function",而其他浏览器在这种情况下会返回"object"。
标签:
原文地址:http://www.cnblogs.com/pengjielee/p/4440970.html