①obj instanceof Array / Object
②Array.prototype.isPrototypeOf(obj)
③Object.prototype.toString.call(obj)
④Array.isArray(obj)
实例:
//typeof() ?【原始类型:可分辨;引用类型:object】console.log(typeof([])); ?//objectconsole.log(typeof({})); ?//object//①obj instanceof 构造函数名/类型名console.log([] instanceof Array); ?//trueconsole.log({} instanceof Array); ?//false//②Array.prototype.isPrototypeof(obj)console.log(Array.prototype.isPrototypeOf([])); ?//trueconsole.log(Array.prototype.isPrototypeOf({})); ?//false//③Object.prototype.toString.call(obj)console.log(Object.prototype.toString.call([])); ?//[object Array]console.log(Object.prototype.toString.call({})); ?//[object Object]//④ES5: Array.isArray(obj) 【兼容性】console.log(Array.isArray([])); ?//trueconsole.log(Array.isArray({})); ?//false
js 判断值为Array or Object的方法
原文地址:http://www.cnblogs.com/minigrasshopper/p/8058957.html