可以使用:instanceof
function isArray(obj) { return obj instanceof Array; } function isObject(obj){ return obj instanceof Object; } var arr=[1,2,3,4,5,6]; var object={name:'袁文军'}; console.log(isArray(arr),isObject(object));
另一种方法:
function isArray(obj) { return Object.prototype.toString.call(obj)=='[object Array]'; } function isObject(obj){ return Object.prototype.toString.call(obj)=='[object Object]'; } var arr=[1,2,3,4,5,6]; var object={name:'袁文军'}; console.log(isArray(arr),isObject(object));