近期做项目对JavaScript的类型判断用到了很多,由于有时候用typeof 有时候用 === 有时候用constructor,弄得自己有些头大,于是做了下面最简单的一个代码测试。从测试结果我得出,要判断严格判断一个变量的类型应该用 === + typeof,=== + typeof 可以准确判断出5种类型,分别是:number, string, boolean, undefined, function 这个组合无法分辨object和array ,如果要严格区分Array 和Object就只有用 constructor + === 来判断了,一个undefined的变量是不能取constructor的,会报错!
这里要注意一下,用typeof 返回的是一个字符串,而且都是小写,所以用typeof的时候得这样写,如判断变量是否为string :if(typeof xx === "string") , 而 constructor返回的是一个对象 ,所以用constructor的时候得这样写,如判断变量xx是否为 Array if(xx && xx.constructor === Array) 前面的if (xx && 是首先判断 xx, 保证xx不为 undefined 且 不为null)
Ok 现在上代码
<
script
>
var a = 1 , b = " 1 " , c = true ;
var d, e = [], f = {},g = null ;
var w = function (s) {document.write(s + " <br /> " );};
//首先声明了一些变量和一个函数用于下面的测试
/**********测试开始**********/
w( " typeof a: " + typeof a);
w( " typeof b: " + typeof b);
w( " typeof c: " + typeof c);
w( " typeof d: " + typeof d);
w( " typeof e: " + typeof e);
w( " typeof f: " + typeof f);
w( " typeof w: " + typeof w);
w( " <br>a.constructor: " + a.constructor);
w( " b.constructor: " + b.constructor);
w( " c.constructor: " + c.constructor);
w( " d.constructor: 会报错 " );
w( " e.constructor: " + e.constructor);
w( " f.constructor: " + f.constructor);
w( " w.constructor: " + w.constructor);
w( " g.constructor: 会报错 " );
if (e && e.constructor === Array) w( " <br />(e && e.constructor === Array) works " );
if (e && e.constructor == Array) w( " (e && e.constructor == Array) works " );
if (w && w.constructor === Function) w( " (w && w.constructor === Function) works " );
if (w && w.constructor == Function) w( " (w && w.constructor == Function) works " );
if (d == undefined) w( " <br />(d==undefined) works " );
if (d == null )w( " (d==null) works " );
if (d === undefined) w( " (d===undefined) works " );
if ( null == undefined) w( " (null == undefined) works " )
if ( null === undefined) w( " (null === undefined) works " ); else w( " (null === undefined) does not work " )
if (a == b) w( " (1==\ " 1 \ " ) works " )
if (a === b) w( " (1===\ " 1 \ " ) works " ); else w( " (1===\ " 1 \ " ) does not work " );
</ script >
我这边用测试结果如下:
var a = 1 , b = " 1 " , c = true ;
var d, e = [], f = {},g = null ;
var w = function (s) {document.write(s + " <br /> " );};
//首先声明了一些变量和一个函数用于下面的测试
/**********测试开始**********/
w( " typeof a: " + typeof a);
w( " typeof b: " + typeof b);
w( " typeof c: " + typeof c);
w( " typeof d: " + typeof d);
w( " typeof e: " + typeof e);
w( " typeof f: " + typeof f);
w( " typeof w: " + typeof w);
w( " <br>a.constructor: " + a.constructor);
w( " b.constructor: " + b.constructor);
w( " c.constructor: " + c.constructor);
w( " d.constructor: 会报错 " );
w( " e.constructor: " + e.constructor);
w( " f.constructor: " + f.constructor);
w( " w.constructor: " + w.constructor);
w( " g.constructor: 会报错 " );
if (e && e.constructor === Array) w( " <br />(e && e.constructor === Array) works " );
if (e && e.constructor == Array) w( " (e && e.constructor == Array) works " );
if (w && w.constructor === Function) w( " (w && w.constructor === Function) works " );
if (w && w.constructor == Function) w( " (w && w.constructor == Function) works " );
if (d == undefined) w( " <br />(d==undefined) works " );
if (d == null )w( " (d==null) works " );
if (d === undefined) w( " (d===undefined) works " );
if ( null == undefined) w( " (null == undefined) works " )
if ( null === undefined) w( " (null === undefined) works " ); else w( " (null === undefined) does not work " )
if (a == b) w( " (1==\ " 1 \ " ) works " )
if (a === b) w( " (1===\ " 1 \ " ) works " ); else w( " (1===\ " 1 \ " ) does not work " );
</ script >
测试结果:
typeof a: number
typeof b: string
typeof c: boolean
typeof d: undefined
typeof e: object
typeof f: object
typeof w: function
a.constructor: function Number() { [native code] }
b.constructor: function String() { [native code] }
c.constructor: function Boolean() { [native code] }
d.constructor: 会报错
e.constructor: function Array() { [native code] }
f.constructor: function Object() { [native code] }
w.constructor: function Function() { [native code] }
g.constructor: 会报错
(e && e.constructor === Array) works
(e && e.constructor == Array) works
(w && w.constructor === Function) works
(w && w.constructor == Function) works
(d==undefined) works
(d==null) works
(d===undefined) works
(null == undefined) works
(null === undefined) does not work
(1=="1") works
(1==="1") does not work
PS : 会报错是我加上去的,测试过一定会报错的,所以用中文字代替了
typeof a: number
typeof b: string
typeof c: boolean
typeof d: undefined
typeof e: object
typeof f: object
typeof w: function
a.constructor: function Number() { [native code] }
b.constructor: function String() { [native code] }
c.constructor: function Boolean() { [native code] }
d.constructor: 会报错
e.constructor: function Array() { [native code] }
f.constructor: function Object() { [native code] }
w.constructor: function Function() { [native code] }
g.constructor: 会报错
(e && e.constructor === Array) works
(e && e.constructor == Array) works
(w && w.constructor === Function) works
(w && w.constructor == Function) works
(d==undefined) works
(d==null) works
(d===undefined) works
(null == undefined) works
(null === undefined) does not work
(1=="1") works
(1==="1") does not work