js如何区分数组和对象?
方法一:通过 ES6 中的 Array.isArray 来识别
Array.isArray([]) //true
Array.isArray({}) //false
方法二:通过 instanceof 来识别
[] instanceof Array //true
{} instanceof Array //false
方法三:通过调用 constructor 来识别
{}.constructor //返回 object
[].constructor //返回 Array
方法四:通过 Object.prototype.toString.call 方法来识别
Object.prototype.toString.call([]) //["object Array"]
Object.prototype.toString.call({}) //["object Object"]
浙公网安备 33010602011771号