简体   繁体   English

typeof 对象但不是数组

[英]typeof object but not array

I am looking for a quick to check to determine this我正在寻找快速检查以确定这一点

function isPlainObject(input){
   return !Array.isArray(input) && typeof input === 'object'
}

is there a shorter check I can use to determine the input is like this是否有更短的检查我可以用来确定输入是这样的

{}

but not an array但不是数组

[]

or other possible structures that still checkout as typeof 'object'?或其他可能的结构仍作为“对象”类型结帐?

? ?

It is not quicker, but more precise, with a check for falsy values, like null , which is an object.它不是更快,而是更精确,检查虚假值,如null ,它是一个对象。

function isPlainObject(input){
   return input && !Array.isArray(input) && typeof input === 'object';
}

If you want to check if an object is a "plain" object, ie inherits directly from Object.prototype , then you should check for that.如果你想检查一个对象是否是一个“普通”对象,即直接从Object.prototype继承,那么你应该检查它。

Eg the following first tests if value has Object anywhere on it's prototype chain (and hence will not throw an error for getPrototypeOf ), then checks if its immediate [[prototype]] is Object.prototype :例如,下面首先测试value在它的原型链上的任何地方是否有Object (因此不会为getPrototypeOf抛出错误),然后检查它的直接[[prototype]]是否是Object.prototype

 function isPlainObject(value) { return value instanceof Object && Object.getPrototypeOf(value) == Object.prototype; } // Some tests [[1,2], // Array {}, // Plain object null, // null document.createElement('div'), // host object function(){}, // function object console // host objet ].forEach(function(value) { console.log(value + ': ' + isPlainObject(value)); });

Edit编辑

If you want to test that the input is some extended object but not a Function, etc. that is much less efficient, eg test against some list of objects that you want to avoid:如果您想测试输入是某个扩展对象而不是函数等,那么效率要低得多,例如针对您要避免的某些对象列表进行测试:

 function isJustObj(obj) { var notThese = [Function, Array, Date]; if (obj instanceof Object) { return !notThese.some(function(o) { return obj instanceof o; }); } return false; } function Foo(){} var tests = {'Array: ':[], 'Object: ' : {}, 'Foo instance:' : new Foo(), 'Function: ' : function(){}, 'Date: ' : new Date(), 'Host obj: ' : document.createElement('div') }; Object.keys(tests).forEach(function(test) { console.log(test + isJustObj(tests[test])); })

Note that this strategy sees if the value is some kind of Object, then tests whether it's an instance of a particular set of constructors.请注意,此策略查看该值是否为某种 Object,然后测试它是否是一组特定构造函数的实例。 This list of things to exclude can become very large since it's not possible in any reasonable way to rule out host objects which, by their very nature, can be indistinguishable from built-in objects based on some general test (see Is there an environment-agnostic way to detect Javascript Host Objects? ).要排除的事物列表可能会变得非常大,因为不可能以任何合理的方式排除宿主对象,这些对象本质上可能与基于某些常规测试的内置对象无法区分(请参阅是否存在环境-检测 Javascript 主机对象的不可知方法? )。

Eg例如

console.log instanceof Function // true
console instanceof Object       // true
isPlainObject(console)          // false

So you either check if Object.prototype is the immediate [[Prototype]] or create a long list of constructors to test against.所以你要么检查 Object.prototype 是否是直接的[[Prototype]]或者创建一长串构造函数来测试。 That list will go out of date very quickly given the variety of host environments available and the freedom for implementors to extend it.鉴于可用主机环境的多样性以及实现者扩展它的自由,该列表将很快过时。 Also, you need to test every member of the host object set before trying to use it as it may not exist for the particular host on which the code is running.此外,您需要在尝试使用主机对象集之前测试它的每个成员,因为对于运行代码的特定主机,它可能不存在。

Javascript 数组被认为是对象,因此 typeof 在数组的情况下将始终是对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM