【发布时间】:2021-08-26 16:51:50
【问题描述】:
我在 Node JS 中有一个简单的类
class Animal {
constructor(name) {
this.name = name;
// more fields ...
}
}
let objects = [];
objects.push(new Animal('Dog'));
objects.push(new Animal('Cat'));
objects.push(new Animal('Dinosaur'));
console.log(objects);
检查这个数组是否是 instanceof Animal objects 的最佳方法是什么,而不是循环检查每个元素的 instanceof ?
【问题讨论】:
-
您的问题不清楚。
check if this array is instanceof Animal objects你是说check if this array only contains instanceof Animal objects吗? -
@ikhvjs:没错!
-
那么,你仍然需要遍历数组中的所有元素进行检查。
标签: javascript node.js ecmascript-6 es6-class