JS typeof 运算符检测结果惊讶发现

文章作者:中山市飞娥软件工作室「Feiesoft.com」  浏览次数:1789 次  更新日期:2013-10-29

<pre><script type="text/javascript">
 //var nul = NULL;
 var num = 2014.1;
 var str = "feiesoft.com";
 var boo = false;
 var arr = new Array();
 var fun = function(){};
 var obj = {};
 var now = new Date();
 var win = this;
 //未定义变量
 document.writeln("typeof NULL = " + typeof NULL);// typeof NULL = undefined
 //null在JS中是一个对象
 document.writeln("typeof null = " + typeof null);// typeof null = object(注意!!)
 //未定义变量
 document.writeln("typeof undefined = " + typeof undefined);// typeof undefined = undefined
 //数字类型
 document.writeln("typeof num = " + typeof num);// typeof num = number
 //字符串类型
 document.writeln("typeof str = " + typeof str);// typeof str = string
 //布尔类型
 document.writeln("typeof boo = " + typeof boo);// typeof boo = boolean
 //数组
 document.writeln("typeof arr = " + typeof arr);// typeof arr = object(注意!!)所有数组都是对象
 //JS函数
 document.writeln("typeof fun = " + typeof fun);// typeof fun = function(注意!!)所有函数都是对象,但类型是 "function"
 //一个空对象
 document.writeln("typeof obj = " + typeof obj);// typeof obj = object
 //Date对象
 document.writeln("typeof now = " + typeof now);// typeof now = object
 //再次测试一个未定义变量
 document.writeln("typeof vip = " + typeof vip);// typeof vip = undefined
 //Number是数字类型的构造函数,其实也是一个对象。
 document.writeln("typeof Number = " + typeof Number);// typeof Number = function
 //Number是数字类型的构造函数,下同。
 document.writeln("typeof String = " + typeof String);// typeof String = function
 document.writeln("typeof Boolean = " + typeof Boolean);// typeof Boolean = function
 document.writeln("typeof Array = " + typeof Array);// typeof Array = function
 document.writeln("typeof Object = " + typeof Object);// typeof Object = function
 //this在此处的运行环境下是window对象的引用
 document.writeln("typeof this = " + typeof this);// typeof this = object(this = window)
 document.writeln("typeof win = " + typeof win);// typeof win = object
 //一个空对象直接量
 document.writeln("typeof {} = " + typeof {});// typeof {} = object
 //一个空数组直接量
 document.writeln("typeof [] = " + typeof []);// typeof [] = object
 //一个以s字符结尾的正则表达式对象直接量
 document.writeln("typeof /s$/ = " + typeof /s$/);// typeof /s$/ = object
 //Math是JS内核预定义的对象
 document.writeln("typeof Math = " + typeof Math);// typeof Math = object
 //Function是函数的原型
 document.writeln("typeof Function = " + typeof Function);// typeof Function = function
 //一个数字形的常量
 document.writeln("typeof Math.PI = " + typeof Math.PI);// typeof Math.PI = number
 //一个类(JS中类也是对象)的方法(函数)
 document.writeln("typeof Math.random = " + typeof Math.random);// typeof Math.random = function
 
</script></pre>