'How to detect if a variable is a pure javascript object
I have a function I need to pass an object to. I use typeof
operator to make a check before processing. But looking at this link, it appears that many javascript instances, such as array or regex, are typed as Objects.
I need my argument to be a pure object (like this : {key: value, . . .}
).
Is their any way I can check if a variable is pure object, without having to run specific test for each Object instance, like Array.isArray()
?
Solution 1:[1]
To achieve expected result, use below option of finding constructor name to check if variable is pure javascript Object or not
As per MDN,
All objects (with the exception of objects created with Object.create(null)) will have a constructor property. Objects created without the explicit use of a constructor function (i.e. the object and array literals) will have a constructor property that points to the Fundamental Object constructor type for that object.
Please refer this link for more details on constructor property - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
var x = {a:1,b:2};
var y = [1,2,3];
console.log(x.constructor.name === "Object")//x.constructor.name is Object
console.log(y.constructor.name === "Object")//y.constructor.name is Array
Solution 2:[2]
You can check prototypes:
function isPureObject(input) {
return null !== input &&
typeof input === 'object' &&
Object.getPrototypeOf(input).isPrototypeOf(Object);
}
console.log(isPureObject({}));
console.log(isPureObject(new Object()));
console.log(isPureObject(undefined));
console.log(isPureObject(null));
console.log(isPureObject(1));
console.log(isPureObject('a'));
console.log(isPureObject(false));
console.log(isPureObject([]));
console.log(isPureObject(new Array()));
console.log(isPureObject(() => {}));
console.log(isPureObject(function () {}));
Solution 3:[3]
the shortest version
const isObj = o => o?.constructor === Object;
found here: https://stackoverflow.com/a/61684890/7138254
Solution 4:[4]
Detection of a plain object is a bit problematic because of the definition of "plain" or "pure". I use the following method to detect plain objects. I never use x instanceof NodeList
type validations because they do not work on cross-frame situations. (each window or frame has its own instance)
I think this is the simplest and most effective way of detecting plain objects.
function isPlainObject(o) {
var c = Object.prototype.toString.call(o) == '[object Object]'
&& o.constructor && o.constructor.name=="Object";
return c === true;
}
function myTestFunction(){
/* ... */
}
class myTestClass{
/* ... */
}
console.log( isPlainObject({"a":1,"b":2}) ); //true
console.log( isPlainObject({}) ); //true
console.log( isPlainObject(null) ); //false
console.log( isPlainObject("my string") ); //false
console.log( isPlainObject("") ); //false
console.log( isPlainObject([1,2,3]) ); //false
console.log( isPlainObject(document.querySelectorAll("*")) ); //false
console.log( isPlainObject(new RegExp(/[a-z]+/)) ); //false
console.log( isPlainObject(myTestFunction) ); //false
console.log( isPlainObject(new myTestFunction()) ); //false
console.log( isPlainObject(myTestClass) ); //false
console.log( isPlainObject(new myTestClass()) ); //false
Solution 5:[5]
If you just want to check for "normal" pure objects (the ones created by {...}
, JSON.parse(...)
, etc.), then this function will work
function is_pure_object(val) {
return val ? Object.getPrototypeOf(val)==Object.prototype : false
}
If you also need to handle objects without a prototype (very rare, only created by Object.create(null)
), then you need to do an additional check:
function is_pure_object2(val) {
if (!val)
return false
let proto = Object.getPrototypeOf(val)
return proto == Object.prototype || proto == null
}
Notes about other solutions:
- Checking
val.constructor
is not reliable:- fails on values like
{constructor: 1}
- fails on
Object.create(null)
- error if
Object.prototype.constructor
is modified
- fails on values like
Object.getPrototypeOf(val).isPrototypeOf(Object)
is also not ideal:- error on
Object.create(null)
- error if
Object.prototype.isPrototypeOf
is modified
- error on
(Object.prototype
being modified is unlikely, but I've dealt with it before)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Lee Taylor |
Solution 2 | |
Solution 3 | Igor Sukharev |
Solution 4 | |
Solution 5 | 12Me21 |