如何区分数组和对象?
参考答案
Array.isArray(x)—— 最可靠(跨 realm)Object.prototype.toString.call(x)→'[object Array]'x instanceof Array—— 同 realmx.constructor === ArrayArray.isArray(x) ? 'array' : typeof x === 'object' && x !== null ? 'object'- 结构判断(不可靠,只作辅助):有
length且索引连续
注意:类数组(arguments/NodeList)不是数组,用 isArray 返回 false。