31

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?

Michael B. Currie
14.8k11 gold badges48 silver badges61 bronze badges
asked Oct 7, 2011 at 14:47
2
  • 1
    The answer to this puzzle is that Function.__proto__.__proto__ === Object.prototype. But you won't get there through Function.constructor.prototype, because that was set to undefined (or not set at all). Commented Nov 30, 2012 at 17:56
  • +1 I always think the most straightforward way to explain something is to draw a picture/diagram and let it talk. Thanks for attaching the diagram here! Commented Mar 22, 2013 at 2:40

2 Answers 2

22

First, the function Empty() {} representation is V8 stuff.

In V8, the Function.prototype object has "Empty" as the value of the Function.prototype.name property, so I guess you are probably using the Chrome's Developer Console, and it displays the name of the function in this way.

The name property of function objects is non-standard (not part of ECMA-262), that's why we see differences between implementations.

Now, Function.prototype is a function, that returns always undefined and can accept any number of arguments, but why?. Maybe just for consistency, every built-in constructor's prototype is like that, Number.prototype is a Number object, Array.prototype is an Array object, RegExp.prototype is a RegExp object, and so on...

The only difference (for example, between any function object and Function.prototype) is that obviously Function.prototype inherits from Object.prototype.

it seems everything in javascript start from Object.prototype, am I right about that?

Well, you're right Object.prototype is 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 like Object.prototype is), and form another inheritance chain, e.g.:

var parent = Object.create(null),
 child = Object.create(parent);
Object.prototype.isPrototypeOf(parent); // false
Object.getPrototypeOf(parent); // null
Object.getPrototypeOf(Object.prototype); // null
answered Oct 7, 2011 at 14:56
Sign up to request clarification or add additional context in comments.

2 Comments

Actually I am still but curious to know about why consoles usually shows the typeof Function.prototype as Function and all other types RegExp, String ,Number as Object . I know you have mentioned it but for consistency ?? Please shed some more light on it
@Gurjit, typeof Function.prototype == 'function' because the typeof operator returns 'function' when you use it on a "callable object" (a function). And you can clearly invoke it: Function.prototype() === undefined.
8

To integrate CMS excellent answer:

it seems everything in javascript start from Object.prototype, am I right about that?

Absolutely, objects in JavaScript are chained up to the basic Object. The chain of inheritance works at runtime, so if a base object is modified, everything chained to it will inherit the modifications instantly. If an Object doesn't have a method or a property, the Javascript implementation will follow the prototype chain until it finds it or it will fail.

__proto__ is a non standard accessor to the prototype, which is supported across browsers, but not by IE. Anyway is not meant to be used by application code.

answered Oct 7, 2011 at 15:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.