JavaScript的数据
JavaScript的数据分为两种:简单数据和复杂数据。简单数据包含number,string,boolean,undefined和null这五种;复杂数据只有一种即object。
万能的typeof
我们先测试一下通过typeof来获取简单数据类型。什么也别说了,上代码是王道:
01 |
//
获取变量obj的数据类型 |
02 |
function getType(obj)
{ |
03 |
return typeof (obj); |
04 |
} |
05 |
/*常量获取类型*/ |
06 |
alert(getType(1)); //number |
07 |
alert(getType( "jeff
wong" )); //string |
08 |
alert(getType( true )); //boolean |
09 |
alert(getType(undefined)); //undefined |
10 |
alert(getType( null )); //object |
11 |
/*变量获取类型*/ |
12 |
var num
= 1; |
13 |
var str
= "jeff
wong" ; |
14 |
var flag
= true ; |
15 |
var hell
= undefined; |
16 |
var none
= null ; |
17 |
alert(getType(num)); //number |
18 |
alert(getType(str)); //string |
19 |
alert(getType(flag)); //boolean |
20 |
alert(getType(hell)); //undefined |
21 |
alert(getType(none)); //object |
正如你所看到的那样,通过typeof运算符,前面四个简单数据类型完全在意料之中,但是typeof null却返回object。应该注意到,null是null类型的唯一值,但null并不是object,具有null值的变量也并非object,所以直接通过typeof,并不能正确得到null类型。 要正确获取简单数据类型,只要在getType的地方加点改进就可以了:
1 |
function getType(obj)
{ |
2 |
return (obj
=== null )
? "null" : typeof (obj); |
3 |
} |
接着来试一下复杂数据类型object:
01 |
function Cat()
{ |
02 |
} |
03 |
Cat.prototype.CatchMouse
= function ()
{ |
04 |
//do
some thing |
05 |
} |
06 |
//
获取变量obj的数据类型 |
07 |
function getType(obj)
{ |
08 |
return (obj
=== null )
? "null" : typeof (obj); |
09 |
} |
10 |
var obj
= new Object(); |
11 |
alert(getType(obj)); //object |
12 |
var func
= new Function(); |
13 |
alert(getType(func)); //function |
14 |
var str
= new String( "jeff
wong" ); |
15 |
alert(getType(str)); //object |
16 |
var num
= new Number(10); |
17 |
alert(getType(num)); //object |
18 |
var time
= new Date(); |
19 |
alert(getType(time)); //object |
20 |
var arr
= new Array(); |
21 |
alert(getType(arr)); //object |
22 |
var reg
= new RegExp(); |
23 |
alert(getType(reg)); //object |
24 |
var garfield
= new Cat(); |
25 |
alert(getType(garfield)); //object |
我们看到,除了Function(请注意大小写)返回了function,不管是javascript的常见内置对象Object,String或者Date等等,还是自定义function,通过typeof返回的无一例外,通通都是object。但是对于自定义function,我们更愿意得到它的“庐山真面目”(示例中即Cat,而非object),而显然,typeof不具备这种转换处理能力。
constructor,想大声说爱你
既然万能的typeof也有无解的时候,那么我们怎么判断一个变量是否是自定义的function实例呢?我们知道,javascript的所有对象都有一个constructor属性,这个属性可以帮我们判断object数据类型,尤其是对自定义function同样适用:
1 |
var obj
= "jeff
wong" ; |
2 |
alert(obj.constructor
== String); //true |
3 |
obj
= new Cat(); |
4 |
alert(obj.constructor
== Cat); //true |
但是,下面的代码您也可以测试一下:
01 |
//alert(1.constructor);
//数字常量 出错 数字常量无constructor |
02 |
var num
= 1; |
03 |
alert(num.constructor
== Number); //true |
04 |
alert( "jeff
wong" .constructor
== String); //true |
05 |
var str
= "jeff
wong" ; |
06 |
alert(str.constructor
== String); //true |
07 |
var obj= null ; |
08 |
alert(obj.constructor); //null没有constructor属性 |
09 |
none
= undefined; |
10 |
alert(obj.constructor); //undefined没有constructor属性 |
实验证明,数字型常量,null和undefined都没有constructor属性。
下面的代码或许还能有点启发和挖掘作用:
01 |
function Animal()
{ |
02 |
} |
03 |
function Cat()
{ |
04 |
} |
05 |
Cat.prototype
= new Animal(); |
06 |
Cat.prototype.CatchMouse
= function ()
{ |
07 |
//do
some thing |
08 |
} |
09 |
var obj
= new Cat(); |
10 |
alert(obj.constructor
== Cat); //false
?? |
11 |
alert(obj.constructor
== Animal); //true
理解 |
原来对于原型链继承的情况,constuctor也不那么好使了。那怎么办?
直观的instanceof
嘿嘿,有请instanceof隆重登场。看它的命名,好像是获取某一个对象的实例,也不知这样理解对不对?不管怎样,我们还是动手改进上面的代码测试一下先:
01 |
function Animal()
{ |
02 |
} |
03 |
function Cat()
{ |
04 |
} |
05 |
Cat.prototype
= new Animal(); |
06 |
Cat.prototype.CatchMouse
= function ()
{ |
07 |
//do
some thing |
08 |
} |
09 |
var garfield
= new Cat(); |
10 |
alert(garfield instanceof Cat); //true
毫无疑问 |
11 |
alert(garfield instanceof Animal); //true
可以理解 |