0

After the following code has been parsed by a JavaScript interpreter:

function F() {}

...is the following correct?

enter image description here

Ben Aston
  • 53,718
  • 65
  • 205
  • 331

2 Answers2

0

After executing function F() {} the object layout is as follows:

+--------------------+
|        null        |
+--------------------+
          ^
          |              [[proto]]
[[proto]] |    +-----------------------------------+
          |    |                                   |
          |    v                                   |
+--------------------+  constructor  +----------+  |
|  Object.prototype  |-------------->|  Object  |  |
+--------------------+               +----------+  |
          ^                               |        |
          |              [[proto]]        |        |
[[proto]] |    +--------------------------+        |
          |    |                                   |
          |    v                                   |
+--------------------+  constructor  +----------+  |
|                    |-------------->|          |  |
| Function.prototype |   [[proto]]   | Function |  |
|                    |<--------------|          |  |
+--------------------+               +----------+  |
               ^                                   |
               |         [[proto]]                 |
               +--------------------------+        |
                                          |        |
                                          |        |
+--------------------+  constructor  +----------+  |
|    F.prototype     |-------------->|    F     |  |
+--------------------+               +----------+  |
          |                                        |
          |                                        |
          +----------------------------------------+

It's really simple:

  1. All functions inherit from Function.prototype (i.e. the [[proto]] property of Object, Function and F, which are all functions, is Function.prototype).
  2. The constructor property of each Foo.prototype is Foo itself (i.e. Object.prototype.constructor is Object, Function.prototype.constructor is Function & F.prototype.constructor is F).
  3. Object.prototype inherits from null whereas Function.prototype and F.prototype inherit from Object.prototype.

Hope that helps.

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
0

I ended-up writing a blog post on my investigation into the top of the object graph in JavaScript.

https://medium.com/@benastontweet/javascript-inheritance-42d2216e9cf1

Ben Aston
  • 53,718
  • 65
  • 205
  • 331