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
188 views
in Technique[技术] by (71.8m points)

javascript - Correct prototype chain for Function

What is the correct output (meaning correct by the ECMA standard) of the following program?

function nl(x) { document.write(x + "<br>"); }
nl(Function.prototype);
nl(Function.prototype.prototype);
nl(Function.prototype.prototype == Object.prototype);
nl(Function.prototype.prototype.prototype);

Chrome and IE6 agree in saying:

function Empty() {}
null for Chrome / undefined for IE6
false

and then crashing.

Mozilla outputs:

function () { }
[object Object]
false
undefined

Are either of these correct? It seems that the Mozilla one does better, but that the best output is

function () { }
[object Object]
true
undefined
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Function.prototype

From ECMAScript Language Specification:

15.3.3.1 Function.prototype

The initial value of Function.prototype is the Function prototype object (section 15.3.4).

15.3.4 Properties of the Function Prototype Object

The Function prototype object is itself a Function object (its [[Class]] is "Function") that, when invoked, accepts any arguments and returns undefined. The value of the internal [[Prototype]] property of the Function prototype object is the Object prototype object (section 15.3.2.1).

It is a function with an “empty body”; if it is invoked, it merely returns undefined. The Function prototype object does not have a valueOf property of its own; however, it inherits the valueOf property from the Object prototype Object.

I get this output:

  • Opera: function () { [native code] }
  • Chrome: function Empty() {}
  • IE7: function prototype() { [native code]}
  • FF3: function () { }

Chrome and IE7 has named their functions, Opera and IE7 tells you that it will not reveal the implementation. They all agree on this:

nl(typeof Function.prototype); //function

Compare this to:

nl(typeof Object.prototype); //object
nl(typeof Array.prototype); //object
nl(typeof String.prototype); // object

Function.prototype.prototype

I get undefined from Opera and IE7, null from Chrome and [object Object] from FF3. Who is right? Since "The Function prototype object is itself a Function object" shouldn't it be a circular reference to itself? To avoid the circular reference they have chosen different ways. I don't know if there is a standard for that or if it is up to the implementation, but I think an Object is right. Btw, here you see the difference between the internal [[prototype]] and the public prototype in action, like you asked in an earlier question!

Function.prototype.prototype == Object.prototype

This is false because it isn't the same object. See above.

Function.prototype.prototype.prototype

Only FF will give you an answer because of their implementation of Function.prototype.prototype returns an Object.

I agree that your proposed output looks more logic.

They do agree on this:

nl(Object.prototype); // [object Object]
nl(Object.prototype.prototype); // undefined

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

...