Closed AciGo closed 5 months ago
The reason is that Object.create(null)
creates an object as follows:
{}
However, an empty object should actually look like this:
{
[[Prototype]]: Object
}
The current implementation checks whether it is a prototype object rather than a plain object. value.constructor === Object
const isObject = (value: any): value is object => {
return !!value && value.constructor === Object
}
The reason is that
Object.create(null)
creates an object as follows:原因是创建一个对象,如下所示:{}
However, an empty object should actually look like this:但是,空对象实际上应该如下所示:
{ [[Prototype]]: Object }
The current implementation checks whether it is a prototype object rather than a plain object. 当前实现检查它是否是原型对象而不是普通对象。
value.constructor === Object
const isObject = (value: any): value is object => { return !!value && value.constructor === Object }
I want to check if it's a plain object, what method should I use that you provided?
Object.create(null) instanceof Object // false
plain object is not an Object
. Object
is a class instance, while {}
is just a plain object in javascript.
so if you want to check if it is an empty object (no matter it is an Object
or plain object), simply you can do:
function isEmptyPlainObject(value: any): boolean {
return typeof value === 'object' // check if it is an plain object, without checking constructor or instance type
&& Object.keys(value).length === 0; // no size
}
I am not the contributors of radash
. This project is not active maintaining. I will provide this method in my project xdash. Currently, my utility tool is missing documentation. It contains lots of interesting and effective utilities. Welcome to try.
I encountered unexpected behavior with the isObject function. The expected result when calling isObject(Object.create(null)) is true. However, it is currently returning false.