javascript类型检测

来源:互联网 发布:云计算对个人的影响 编辑:程序博客网 时间:2024/05/21 22:51

1、typeof

是和基本类型及function检测,遇到null失效。

typeof "100"  ---->string

typeof 100    ---->number


2、instanceof

是和自定义对象,也可以用来检测原生对象。遇到window以及iframe失效

[1,2] instanceof Array === true

new Object() instanceof object === true


3、{}.toString()

Object.prototype.toString.apply([]) === "[Object Array]"   ------>返回true

Object.prototype.toString.apply({}) === "[Object object]"   ------>返回true

Object.prototype.toString.apply(undefined) === "[Object Undefined]"   ------>返回true

0 0
原创粉丝点击