Function isOrdinaryObject
-
Parameters
- value: unknown
Any value to evaluate.
Returns value is Record<PropertyKey, unknown>
Whether
valueis a non-null object with the runtime string tag'[object Object]'.Remarks
This is a tag-based classification and is not an implementation of the ECMAScript specification's internal distinction between ordinary and exotic objects.
The result of
Object.prototype.toString.callcan be influenced bySymbol.toStringTag. Consequently, an object may opt out of this classification by supplying another tag, and a specialized object may present itself with the tag'Object'.Example
isOrdinaryObject({ value: 1 }); // true
isOrdinaryObject(Object.create(null)); // true
class Configuration {}
isOrdinaryObject(new Configuration()); // true
isOrdinaryObject(new Map()); // false
isOrdinaryObject(new Date()); // false
isOrdinaryObject([]); // falseExample
The distinction from isPlainObject concerns the prototype:
class Configuration {}
const value = new Configuration();
isOrdinaryObject(value); // true
isPlainObject(value); // false - value: unknown
Runtime check for whether a value is an ordinary object:
An ordinary object in this context is a non-null, non-callable object for which
Object.prototype.toString.call(value)returns'[object Object]'.This includes:
{}.new Object().nullprototype.This excludes:
Date,RegExp,Map,Set,Promise,Error,ArrayBuffer,DataView, and typed arrays.This predicate occupies the middle ground between the other object predicates:
isOrdinaryObjectadditionally requires the runtime string tag'[object Object]'. It accepts class instances and objects with custom prototypes, but rejects specialized built-ins.Object.prototypeornull. It therefore rejects class instances and objects with other custom prototypes.Unlike isPlainObject, this function does not inspect or restrict the object's prototype.