如何检查值是否是JavaScript中的对象?
当前回答
我认为有这么多答案的原因是,无论你是否喜欢,很多东西都是javascript中的对象。
您可以像任何其他对象一样迭代数组的“键”。。。
var key,
arr = ['one', 'two', 'three'];
for (key in arr)
console.log(`${key}=${arr[key]}`);
console.log(arr[1]);
console.log(arr['1']);
// 0=one
// 1=two
// 2=three
// two
// two
数组是特殊的(像许多对象一样),因为它有一些通用对象没有的财产/方法(例如长度、forEach等)。
所以这个问题应该是:如何过滤特定类型的对象?
毕竟,我可以很容易地实现自己的数组、正则表达式或符号,它也可以是一个对象,但可能会通过各种“它是真实对象吗?”测试。。。。因为它是一个真实的物体。
所以你想过滤到某些类型的javascript对象。。。
最好的方法是只做特性测试。例如,您是否关心它是否具有长度属性?示例:浏览器中的NodeList看起来像一个数组,可以像数组一样迭代,但不是数组。
无论如何,如果您只想过滤掉特定类型的对象,那么只有您才能定义要过滤的对象。是否要筛选出RegExp?日期大堆浏览器DOM对象?只有你才能决定你的过滤链是什么样子。您可以使用直通开关来构建紧凑的过滤器。
function typeOfIs(val, type) {
return typeof val == type;
}
function constructorIs(val, constructor) {
return val && constructor && val.constructor === constructor;
}
function isObject(val) {
// catch the easy non-object values
if (!typeOfIs(val, 'object'))
return false;
// catch the cases you don't want to consider to be
// "real" objects for your use-case
switch (true) {
case val === null:
case Array.isArray(val):
case typeOfIs(val, 'function'):
case constructorIs(val, RegExp):
return false;
default:
return true;
}
}
function test(val) {
console.log(Object.prototype.toString.call(val)+': '+isObject(val));
}
test(undefined);
test(null);
test(function () {});
test(Symbol('foo'));
test(1);
test(true);
test(false);
test('hello world');
test([]);
test(/.*/g);
test(new Date()); // true (because we didn't filter for it)
test({}); // true
特性测试
但更好的方法可能是询问为什么要筛选,或者只测试给定变量上是否存在您需要/期望的财产/函数。。。如果它们存在,使用变量,不要担心它是某种类型的对象还是另一种类型的对象。如果它们不存在,则抛出一个API错误,即您被传递了一些类型不正确的值(即缺少预期的财产/函数)。
e.g.
if (typeof someVariable.hasOwnProperty == 'function')
// ...
其他回答
哦,我的上帝!我认为这可能比以往任何时候都要短,让我们看看:
短代码和最终代码
函数isObject(obj){返回obj!=null&&obj.constructor.name==“对象”}console.log(isObject({}))//返回trueconsole.log(isObject([]))//返回falseconsole.log(isObject(null))//返回false
解释
退货类型
typeof JavaScript对象(包括null)返回“object”
console.log(类型为null,类型为[],类型为{})
检查其构造函数
检查其构造函数属性会返回带有其名称的函数。
console.log(({}).cconstructor)//返回名为“Object”的函数console.log(([]).cconstructor)//返回名为“Array”的函数console.log((null).cconstructor)//引发错误,因为null实际上没有属性
Function.name简介
Function.name返回函数的只读名称或闭包的“匿名”。
console.log(({}).cconstructor.name)//返回“Object”console.log(([]).cconstructor.name)//返回“Array”console.log((null).cconstructor.name)//引发错误,因为null实际上没有属性
注意:截至2018年,Function.name可能无法在IE中工作https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#Browser_compatibility
lodash有isPlainObject,这可能是许多来到这个页面的人都在寻找的。当给定函数或数组时,它返回false。
请记住,new Date()的类型是“object”。
因此,如果要查找{key:value}对象,日期对象是无效的。
最后:o=>o&&typeof o==“对象”&&!在我看来,(o instance of Date)是对你问题的更好回答。
更新:
这个答案是不完整的,给出了误导性的结果。例如,null也被认为是JavaScript中的对象类型,更不用说其他几种边缘情况。遵循下面的建议,然后转到其他“最支持(和正确!)的答案”:
typeof yourVariable === 'object' && yourVariable !== null
原答覆:
尝试使用typeof(var)和/或var instanceof。
编辑:这个答案给出了如何检查变量财产的思路,但它并不是检查它是否是一个对象的万无一失的方法(毕竟根本没有方法。
如果您想检查对象的原型是否仅来自object。过滤掉字符串、数字、数组、参数等。
function isObject (n) {
return Object.prototype.toString.call(n) === '[object Object]';
}
或作为单个表达式箭头函数(ES6+)
const isObject = n => Object.prototype.toString.call(n) === '[object Object]'