
I see this nice diagram and I’ve done some tests in my Chrome browser, but I don’t know how to explain this:
> Function.prototype
function Empty() {}
> Function.__proto__
function Empty() {}
> typeof(Empty)
"undefined"
What is the function Empty() {}, and why Function.prototype is a function not a object just like Object.prototype?
From the diagram above, it seems everything in JavaScript starts from Object.prototype, am I right about that?
First, the
function Empty() {}representation is V8 stuff.In V8, the
Function.prototypeobject has"Empty” as the value of theFunction.prototype.nameproperty, so I guess you are probably using the Chrome’s Developer Console, and it displays the name of the function in this way.The
nameproperty of function objects isnon-standard(not part of ECMA-262), that’s why we see differences between implementations.Now,
Function.prototypeis a function, that returns alwaysundefinedand can accept any number of arguments, but why?. Maybe just for consistency, every built-in constructor’s prototype is like that,Number.prototypeis aNumberobject,Array.prototypeis anArrayobject,RegExp.prototypeis aRegExpobject, and so on…The only difference (for example, between any function object and
Function.prototype) is that obviouslyFunction.prototypeinherits fromObject.prototype.Well, you’re right
Object.prototypeis the last object of the prototype chain of most objects, but in ECMAScript 5, you can even create objects that doesn’t inherit from anything (just likeObject.prototypeis), and form another inheritance chain, e.g.: