Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

javascript - What is Function's __proto__?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...