概述
四种方法:
typeof,instanceof,constructor,Object.prototype.toString。
普及知识
万物皆对象,是不准确的。
目前有七种数据类型:
string,number,boolean,undefined,null,symbol(es6加入的,表示独一无二的值),object。
object的九个内置对象:String,Number,Boolean,Object,Function,Array,Date,RegExp,Error。
当程序运行时,js引擎会将基本数据类型转成内置对象,如字面量string转换成 String 对象。所以才会有一种万物皆对象的假象,因为js引擎帮你做了。
1. typeof
typeof只能判断六种数据类型,但是null判断为object,原因是二进制null全是0,typeof判断二进制为 3个0 就是object。
typeof 'abc'; // string
typeof 1; // number
typeof true; //boolean
typeof undefined; //undefined
typeof null; //object
typeof Symbol(); // symbol
typeof new Function(); // function
typeof [] ; //object
typeof new Date(); //object
typeof new RegExp(); //object
typeof new Error(); //object
3. constructor
constructor是object的内置对象自带的,所以只能验证九个内置对象。
因为
string、number、boolean,js引擎在运行时,将其转化为object内置对象,所以可以判断;
但是null、undefined没有constructor,还有Symbol.constructor是Function,所以判断不对。
还有
constructor在开发者重写prototype时,constructor会丢失,所以这个判断并不安全。原因在我上篇文章:原型与原型链
'abc'.constructor === String,
new Number(1).constructor == Number,
true.constructor === Boolean,
new Function().constructor === Function,
Symbol.constructor === Function
[].constructor === Array,
{}.constructor === Object,
new Date().constructor === Date,
new RegExp().constructor === RegExp,
new Error().constructor === Error
3. Object.prototype.toString
`toString() 是 Object 的原型方法,调用该方法,默认返回当前对象的[[Class]] 。这是一个内部属性,其格式为 [object type],其中 type 就是对象的类型。
对于 Object 对象,直接调用 toString() 就能返回 [object Object] 。而对于其他对象,则需要通过 call / apply 来调用才能返回正确的类型信息。
Object.prototype.toString.call('abc') ; // [object String]
Object.prototype.toString.call(1) ; // [object Number]
Object.prototype.toString.call(true) ; // [object Boolean]
Object.prototype.toString.call(Symbol()); //[object Symbol]
Object.prototype.toString.call(undefined) ; // [object Undefined]
Object.prototype.toString.call(null) ; // [object Null]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
Object.prototype.toString.call(new Error()) ; // [object Error]
最完美的判断数据类型方案了,但是有一种情况判断不了,那就是判断A 是否为 B 的实例。
function Foo() {}
var f = new Foo();
console.log(Object.prototype.toString.call(f)); // [object Object]
4. instanceof
instanceof 是用来判断 A 是否为 B 的实例,表达式为:A instanceof B,如果 A 是 B 的实例,则返回 true,否则返回 false。
4.1 应用篇
'abc' instanceof String; // false
1 instanceof Number; // false
true instanceof Boolean; // false
[] instanceof Array; // true
[] instanceof Object; // true
{} instanceof Object; // true
new Function() instanceof Function; // true
new Date() instanceof Date; // true
new RegExp() instanceof RegExp; // true
new Error() instanceof Error; // true
- 首先
null,underfined不能instanceof;- 其次
string, number ,boolean判断为false;[] instanceof Array是true,[] instanceof Object也是true;
所以instanceof千万别用来判断数据类型。它的主要作用就是判断Object.prototype.toString实现不了的情况:
function Foo() {}
var f = new Foo();
console.log(f instanceof Foo); // true
4.2 原理篇
我们用一段伪代码来模拟其内部执行过程:
instanceof (A,B) = {
var L = A.__proto__;
var R = B.prototype;
if(L === R) {
// A的内部属性 __proto__ 指向 B 的原型对象
return true;
}
return false;
}
我们来分析一下 [ ]、Array、Object 三者之间的关系:

从原型链可以看出,
[] 的 __proto__ 直接指向Array.prototype,间接指向 Object.prototype,所以按照 nstanceof的判断规则,[] 就是Object的实例。同理,所以内置对象最终都是指向
Object.prototype。
总结:instanceof 只能用来判断两个对象是否属于实例关系, 而不能判断一个对象实例具体属于哪种类型。