【发布时间】:2016-07-02 15:03:50
【问题描述】:
在 SO 和 google 中发现的每个关于检查对象是否为数组的问题最有可能最终得到此解决方案
function isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]'
}
所有其他替代方案都有误报或不完全支持。
来源:
http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
How to detect if a variable is an array
当我阅读 15.4.3.2 部分中的 ES5 规范时,发现函数 Array.isArray 的描述算法在 IE9+、Chrome 5+、Firefox 4+、Opera 10.5+ 和 Safari 5+ 中执行相同的检查,但是这个算法有两个额外的步骤。
function isArray(obj) {
if (typeof obj !== 'object') {
return false;
}
// Here they check only against the [[Class]] part
// and of course they don't have to use the ugly Object.prototype.toString.call
// but this is pretty much the same comparison
if (Object.prototype.toString.call(obj) === '[object Array]') {
return true;
}
return false;
}
现在我的问题是他们为什么要先检查类型?对于仍然具有 [[Array]] 内部类的对象,是否有特殊情况会返回 false?
【问题讨论】:
-
请注意,这些东西基本上已经过时了,ES2015 根本不使用
[[Class]]es。 -
@georg 我知道你的意思,但是直到所有浏览器都完全支持ES2015 并且世界上只有最少数量的浏览器不支持(可能需要十年)学习这是并非完全没用,因为这是我们今天仍在使用的语言,即使我们使用了转译器。
-
只是说,
Array.isArray完全支持所有严肃的浏览器。 -
@Bergi 是的,我知道这是真的。请注意,当我说“直到所有浏览器完全支持 ES2015”时,我指的是 [[Class]] 内部属性,而不是缺少
Array.isArray方法。我已经看到有些人带着像Array.isArray ? Array.isArray(obj) : Object.prototype....这样的 pollyfill 来确定。难以置信,我见过一些环境仍然使用 IE8 o_O。
标签: javascript arrays algorithm types ecmascript-5