根据网易云课堂总结
一,typeof
可以识别标准类型(Null除外),不能识别具体的对象类型(Function除外),typeof是操作符,不是函数,所以后面的括号可有可无
<pre name="code" class="javascript">typeof "abc";//"string"
typeof 12;//"number"
typeof true;//"boolean"
typeof undefined;//"undefined"
typeof null;<span style="font-family: Arial, Helvetica, sans-serif;">//"object",</span><span style="font-family: Arial, Helvetica, sans-serif;">只能判断出空对象是object</span>
typeof {name:"abc"};//"object"
typeof (function(){});//<span style="font-family: Arial, Helvetica, sans-serif;">"function"</span>
typeof (new Date())//"object"
二,instanceof
不能识别标准类型,可以判别内置对象类型,能够判别自定义对象类型及父子类型
//能够判别内置对象类型
[] instanceof Array;//true
[] instanceof Object;//true
/\d/ instanceof RegExp;//true
(new Date()) instanceof Date;//true
(function(){}) instanceof Function;//true
//不能判断标准类型
1 instanceof Number;//false
"abc" instanceof String;//false
true instanceof Boolean;//false
//能判别自定义对象类型及父子类型
//例子1
function Person(age){
this.age = age;
}
function Chinese(age,name){
Person.call(this,age);
this.name = name;
}
var person1 = new Chinese(16,"Xiao Ming");
person1 instanceof Person;//false
person1 instanceof Chinese;//true
//例子2
function Person(age){
this.age = age;
}
function Chinese(age,name){
Person.call(this,age);
this.name = name;
}
Chinese.prototype = new Person();
Chinese.prototype.constructor = Chinese;
var person1 = new Chinese(16,"Xiao Ming");
person1 instanceof Person;//true
person1 instanceof Chinese;//true
三,Object.prototype.toString.call
可以识别标准类型以及内置对象类型,不能识别自定义对象类型
Object.prototype.toString.call ("abc");
"[object String]"
Object.prototype.toString.call (12);
"[object Number]"
Object.prototype.toString.call (true);
"[object Boolean]"
Object.prototype.toString.call (undefined);
"[object Undefined]"
Object.prototype.toString.call (null);
"[object Null]"
Object.prototype.toString.call ({name:"abc"});
"[object Object]"
Object.prototype.toString.call (function(){});
"[object Function]"
Object.prototype.toString.call (new Date());
"[object Date]"
Object.prototype.toString.call ([]);
"[object Array]"
Object.prototype.toString.call (/\d/);
"[object RegExp]"
//不能识别自定义对象类型
function Person(age){
this.age = age;
}
function Chinese(age,name){
Person.call(this,age);
this.name = name;
}
Chinese.prototype = new Person();
Chinese.prototype.constructor = Chinese;
var person1 = new Chinese(16,"Xiao Ming");
Object.prototype.toString.call (person1);
"[object Object]"
//只显示对象类型,去除前面的object和[]
function type(obj){
return Object.prototype.toString.call(obj).slice(8,-1);
}
四,constructor可以判别标准类型(Undefined和Null除外,因为这两个没有构造函数),可以判别内置对象类型,自定义对象类型
"abc".constructor === String//true
(1).constructor === Number//true
true.constructor === Boolean//true
({}).constructor === Object//true
(function(){}).constructor === Function//true
//判别内置对象类型
new Date().constructor === Date;//true
[].constructor === Array;//true
/\d/.constructor === RegExp//true
//判别自定义对象类型
function Person(name){
this.name = name;
}
new Person("Xiao Ming").constructor === Person;//true
//获取对象构造函数名称
function getConstructorName(obj){
return (obj||obj==="") && obj.constructor && obj.constructor.toString().match( /function\s*([^(]*)/ )[1];
//function String() { [native code] }中提取String这个名字
}
总结:
1. typeof 可以识别标准类型(Null除外),不能识别具体的对象类型(Function除外)
2. instanceof 不能识别标准类型,能判别内置对象类型,不能够判别自定义对象类型
3. Object.prototype.toString.call 可以识别标准类型以及内置对象类型,不能识别自定义对象类型4. constructor 可以判别标准类型(Undefined和Null除外),可以判别内置对象类型,自定义对象类型