Object.prototype returns empty object in Node

  • A+
Category:Languages

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.

Object.prototype returns empty object in Node

Object.prototype returns empty object in Node

 


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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: