js中数组和对象的区别方法-爱代码爱编程
可以使用: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));
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/yuanwen52/article/details/105694730