who is the prototype object of global object/scope in javascript?

911 views Asked by At

I tested this in Node shell:

var a = {}
a.__proto__ === Object.prototype // true

global.__proto__ === Object.prototype // false
global.__proto__.__proto__ === Object.prototype // true

global.constructor.name // 'Object'
global.__proto__.constructor.name // 'Object'

My question:

  1. why global object (global scope) 's prototype is NOT Object.prototype?

  2. why global's constructor name displayed as 'Object' but it's prototype is not Object.prototype?

2

There are 2 answers

1
wordSmith On

every object has an Object at the head of their prototype-chain right before null. The chain is as follows: null->Object->A->B->C->D

The reason global.__proto__.__proto__ === Object.prototype // true works is because it follows null and then Object's prototype, which is the constructor for the new object. Global is the environment.

Hope this helps!

0
user2864740 On

If talking about the Global Object which is not the same as the Global Scope (although in the global scope this === theGlobalObject) the specification states:

.. The values of the [[Prototype]] and [[Class]] internal properties of the global object are implementation-dependent.

To find an answer which is specific to Node.js, the implementation (or documentation on why such a decision was made) would need to be consulted. However there is no rationale/requirement provided by the specification.