确定哪些对象是JavaScript中的is complicated,并确定哪些对象是数组,具有hacky solution的内容。幸运的是,它设法在以下两种情况下工作:
Object.prototype.toString.call([]); // [object Array]
Object.prototype.toString.call(new Array()); // [object Array]
很好,没有[object Object]
在眼前!可悲的是,这种方法仍然失败了:
var arr = Object.create(Array.prototype);
Object.prototype.toString.call(arr); // [object Object]
这令人沮丧,所以至少可以这么说。我的arr
对象具有数组的所有方法,它的功能类似于数组,并且出于所有目的,它是数组。然而,JavaScript并没有提供识别它的工具。
有没有办法弄清楚一个对象是否继承了特定的原型?我想你可以像这样迭代原型:
function inherits(obj, proto) {
while (obj != null) {
if (obj == proto) return true;
obj = Object.getPrototypeOf(obj);
}
return false;
}
inherits(Object.create(Array.prototype), Array.prototype); // true
但感觉有点黑客。有没有更干净的方法?
答案 0 :(得分:2)
instanceof运营商怎么样?它会为您的所有情况返回true
:
[] instanceof Array //true
new Array() instanceof Array //true
Object.create(Array.prototype) instanceof Array //true
然而:
Object.create(Array.prototype) instanceof Object //also true
所以要小心。
答案 1 :(得分:2)
ECMAScript 5已将Array.isArray()
引入javascript,它提供了一种可靠的检查方式。对于较旧的浏览器,我们通过(引用此book)
function isArray(value) {
if (typeof Array.isArray === "function") {
return Array.isArray(value);
} else {
return Object.prototype.toString.call(value) === "[object Array]";
}
}
但我发现当我们使用Array.isArray
(在chrome中测试)时,内置函数Object.create
无法正常工作。我想出了一个有效的方法:
function isArray(value) {
if (typeof value === "undefined" || value === null) {
return false;
}
do {
if (Object.prototype.toString.call(value) === "[object Array]") {
return true;
}
value= Object.getPrototypeOf(value);
} while (value);
return false;
}
使用它:
var arr = Object.create(Array.prototype);
var arr1 = Object.create(Object.create(Array.prototype));
var arr2 = new Array();
var arr3 = [];
isArray(arr);
isArray(arr1);
isArray(arr2);
isArray(arr3);
答案 2 :(得分:2)
有关继承自Array的问题的最终说明,请参阅http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/。
无论如何,在最简单的情况下,你在做什么
var sort_of_an_array = Object.create(Array.prototype);
您可以使用isPrototypeOf
检查:
Array.prototype.isPrototypeOf(sort_of_an_array)
答案 3 :(得分:0)
检查构造函数怎么样?
function inherits(obj, proto) {
return obj.constructor === proto.constructor;
}
inherits(Object.create(Array.prototype), Array.prototype); // true
答案 4 :(得分:0)
它的功能类似于数组,并且出于所有目的,它是一个数组
没有。它没有自动更新length
属性。请参阅this article为什么不可能继承Array
。
有没有办法弄清楚一个对象是否继承了特定的原型?我想你可以遍历原型,但感觉有点黑客。
这就是如何做到的。比自编函数更简洁的方法是使用instanceof
operator:
arr instanceof Array; // true