While I execute Object.prototype
in browser console, i am getting all the properties and methods available inside Object.prototype
. This is as expected but when i am executing exactly the same thing in NodeJS terminal I am getting an empty object {}
. Could anyone please explain me why its like this? I have attached screenshots of both.
Use the Object. entries() function. It returns an array containing the object's enumerable properties. If it returns an empty array, it means the object does not have any enumerable property, which in turn means it is empty.
New Keyword. The Object constructor creates an object wrapper for a given value. If the value is null or undefined , it will create and return an empty object.
keys method to check for an empty object. const empty = {}; Object. keys(empty). length === 0 && empty.
There are only seven values that are falsy in JavaScript, and empty objects are not one of them. An empty object is an object that has no properties of its own. You can use the Object.
It is because the console.log() in node use util.inspect(), which uses Object.keys() on objects, and it returns enumerable properties only. And Object.prototype
contains non-enumerable properties, that is why it returns empty node.
Similar behavior can be observed in the below snippet, when we console.log(Object.prototype)
it logs an empty {}
;
console.log(Object.prototype);
But when we explicitly define an enumerable property in Object.prototype
it logs an object containing that property :
Object.defineProperty(Object.prototype, 'property1', { value: 42, enumerable : true }); console.log(Object.prototype)
For Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With