一、Object.prototype.toString.call()方法
Object.prototype.toString.call(undefined);
"[object Undefined]"
Object.prototype.toString.call(null);
"[object Null]"
Object.prototype.toString.call('');
"[object String]"
Object.prototype.toString.call({});
"[object Object]"
Object.prototype.toString.call(function(){});
"[object Function]"
Object.prototype.toString.call(123);
"[object Number]"
由以上的例子可以知道:Object.prototype.toString.call()方法是可以判断任何类型的,包括null,undefined等等。
二、instanceof()方法
instanceof的作用只要就是用来检测某构造函数上的原型上是否存在于要检测对象得原型链上。这话说得压根儿就不是人话,直接上例子:
定义两个函数:分别是test1 和 test2
function Test1(){}
function Test2(){}
var t1 = new Test1();
t1 instanceof Test1; //返回值是true, 因为 Object.getPrototypeOf(t1) === Test1.prototpye
t1 instanceof Test2;//返回值就是false, 因为Test2.prototype不在test1的原型链上;
如果我在实例化一个test2,
var t2 = new Test2();
此时,
t2 instanceof Test2// 返回值就会是true, 因为Test2.prototype在test2的原型链上
三、Array.isArray()方法
这个方法其实就是判断你往里传的参数是否是Array数组。
比如说:
Array.isArray([123])
true
Array.isArray(Array.prototype)//其实Array.prototype也是一个数组
true
Array.isArray(new Array())
true
Array.isArray({})
false
Array.isArray('123')
false
Array.isArray(null)
false
Array.isArray(undefined)
false
Array.isArray(function(){})
false
Array.isArray(123)
false
Array.isArray(true)
false
Array.isArray(false)
false