After the following code has been parsed by a JavaScript interpreter:
function F() {}
...is the following correct?
After the following code has been parsed by a JavaScript interpreter:
function F() {}
...is the following correct?
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:
Function.prototype
(i.e. the [[proto]]
property of Object
, Function
and F
, which are all functions, is Function.prototype
).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
).Object.prototype
inherits from null
whereas Function.prototype
and F.prototype
inherit from Object.prototype
.Hope that helps.
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