用instanceof判断数组类型

1.typeof用以获取一个变量的类型,typeof通常只能返回以下几个结果:number,boolean,string,function,object,undefined。咱们能够使用typeof来获取一个变量是否存在,如if(typeof a!="undefined"){},而不要去使用if(a)由于若是a不存在(未声明)则会出错,对于Array,Null等特殊对象使用typeof一概返回object,这正是typeof的局限性。数组

2.若是咱们但愿获取一个对象是不是数组,或判断某个变量是不是某个对象的实例则要选择使用instanceofinstanceof用于判断一个变量是否某个对象的实例,如var a=new Array();alert(a instanceof Array);会返回true,同时alert(a instanceof Object)也会返回true;这是由于Arrayobject的子类。再如:function test(){};var a=new test();alert(a instanceof test)会返回truespa

相关文章
相关标签/搜索