typeof
typeof("xxx") //"string"
typeof(123) //"number"
typeof(true) //"boolean"
typeof(null) //"object"
typeof(undefined) //undefined
typeof({name:'xxx'}) //"object"
typeof(function(){}) //"function"
typeof([1,2,3]) //"object"
typeof(new Date())//"object"
typeof总结:
- 可以识别标准类型(null除外)
- 不能识别具体的对象类型(Function除外)
instanceof
用于检测构造函数的prototype属性是否存在某个实例的原型链上。
[] instanceof Array; //true
var a=function(){}
var b=new a();
b instanceof a; //true
123 instanceof Number; //false
"xxx" instanceof String; //false
instanceof总结:
- 可以识别内置对象,自定义对象
- 不可识别基本数据类型
- 必须同一个作用域,如果存在多个作用域,比如多个frame,判断不出。
Object.prototype.toString.call
Object.prototype.toString.call([]); //"[object Array]"
Object.prototype.toString.call({}); //"[object Object]"
Object.prototype.toString.call(function(){}) //"[object Function]"
Object.prototype.toString.call("123") //"[object String]"
Object.prototype.toString.call(new Date)//"[object Date]"
var a=function(){}
Object.prototype.toString.call(new a)//"[object Object]"
总结:
- 可以判断标准类型和内置对象类型
- 不能识别自定义对象类型
contructor属性
"xxx".constructor===String //true
(1).constructor===Number //true
(true).constructor===Boolean//true
({}).constructor===Object //true
new Date().constructor===Date//true
([]).constructor===Array //true
var a=function(){}
var b=new a();
b.constructor===a; //true
总结:
- 可以判断内置对象类型(undefined和null除外,因为undefined和null没有构造函数)
- 可以判断普通对象类型
- 可以判断自定义对象类型
End
以上是目前,我所了解的,仅供大家参考,如有错误,还望指出,如有补充,还望指点,thank you!!!