0

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?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
matianfu
  • 366
  • 2
  • 6
  • 15
  • Where is `global` from? It's not in standard JavaScript; anyway - [environment] scopes do not have prototypes. – user2864740 Jun 10 '15 at 06:48
  • Do you realize that `global` is something that node.js makes up itself in its environment? It's not a standard part of Javascript. Your data makes it looks like it is an object inherited from Object. – jfriend00 Jun 10 '15 at 06:50
  • @user2864740 I tested this code in both Node shell and SpiderMonkey's JS shell. The results are the same. Changing global to 'this' have the same output. – matianfu Jun 10 '15 at 06:58
  • It's still something those two environments make up for their own use. – jfriend00 Jun 10 '15 at 06:59

2 Answers2

2

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.

user2864740
  • 60,010
  • 15
  • 145
  • 220
0

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!

wordSmith
  • 2,993
  • 8
  • 29
  • 50
  • Kind of, but not really how document and window are the superior-most structures in the DOM. I know that is a bad example, but it is the only js environment example I can think of at the moment. – wordSmith Jun 10 '15 at 07:05