详解js中typeof、instanceof与constructor
typeof返回一个表达式的数据类型的字符串,返回结果为js基本的数据类型,包括number,boolean,string,object,undefined,function.语法为typeof(data) 或 typeof data
instanceof则为判断一个对象是否为某一数据类型,或一个变量是否为一个对象的实例;返回boolean类型
语法为 o instanceof A
以下为综合实例:
1
<script type="text/javascript">
2
<!–
3
alert("typeof(1):" + typeof(1));//number
4
alert("typeof(\"abc\"):" + typeof("abc"));//string
5
alert("typeof(true):" +typeof(true));//boolean
6
alert("typeof(2009-2-4):" + typeof(2009-2-4));//number
7
alert("typeof(\"2009-2-4\"):" + typeof("2009-2-4"));//string
8
alert("typeof(m):" + typeof(m));//undefined
9
var d=new Date();
10
alert("typeof(d):" + typeof(d));//object
11
function Person(){};
12
alert("typeof(Person):" + typeof(Person));//function
13
var a=new Array();
14
alert("typeof(a):" + typeof(a));//object
15
alert("a instanceof Array:" + (a instanceof Array));
16
var h=new Person();
17
var o={};
18
alert("h instanceof Person:" + (h instanceof Person));//true
19
alert("h instanceof Object:" + (h instanceof Object));//true
20
alert("o instanceof Object:" + (o instanceof Object));//true
21
alert(typeof(h));//object
22
//–>
23
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
js中constructor较少使用,如果不是搜索到相关construtor相关的资料,我之前从没有注意到js还有这个函数。
使用typeof的一个不好的地方就是它会把Array还有用户自定义函数都返回为object
1
<script type="text/javascript">
2
<!–
3
var j=2;
4
alert(typeof(j));//number
5
alert("j.constructor:" + j.constructor);//function …
6
alert(typeof(j.constructor));//function
7
//–>
8
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
可以看到js.constructor返回的是一些字符串,大家都应该能看到这是一个function类型,此例为Number()为Number对象的构造函数,Number()用于将其参数转换为数字number类型,并返回转换结果(若不能转换则返回 NaN)。
因此在以后的js判断数据类型时可以使用以下方式来得到其详细数据类型
1
if((typeof o=="object") && (o.constructor==Number)){
2
…
3
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这里还要注意,constructor只能对已有变量进行判断,而typeof则可对未声明变量进行判断(返回undefined)。
原文来自:http://www.51obj.cn/