正确的原型链的功能 [英] Correct prototype chain for Function
问题描述
以下程序的正确输出(通过ECMA标准是否正确)是什么?
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和IE6同意:
Chrome and IE6 agree in saying:
function Empty() {}
null for Chrome / undefined for IE6
false
然后崩溃。
Mozilla输出:
Mozilla outputs:
function () { }
[object Object]
false
undefined
这两个是正确的吗?看起来Mozilla一个更好,但最好的输出是
Are either of these correct? It seems that the Mozilla one does better, but that the best output is
function () { }
[object Object]
true
undefined
推荐答案
你在这里做的不是真正走原型链 - 这个问题可能会帮助您了解实际发生的情况。我没有打扰检查ECMA规格,但这里是我的问题上的采取:
What you're doing here isn't really walking the prototype chain - this question might help you understand what is actually going on. I didn't bother to check the ECMA spec, but here is my take on the issue:
-
/ strong>是函数对象的构造函数
Function is the constructor of function objects
Function.prototype 是所有函数对象继承的原型 - 属于所有 Function 实例所共有的 和应用您检查的实现是一致的,因为它实现为一个函数对象本身(一些指出,ECMA规范要求这一点)
Function.prototype is the prototype from which all function objects inherit - it might contain properties like call and apply which are common to all Function instances; the implementations you checked were consistent in that it is implemented as a function object itself (as some pointed out, the ECMA specification requires this)
Function.prototype.prototype 确实没什么意义,但 Function.prototype 实现为一个函数对象(可能用作构造函数),它应该至少存在;使用 Function.prototype 作为构造函数创建的对象将继承其属性 - 但由于没有理由做这样的疯狂,将其设置为 null , undefined 或空对象是合理的
Function.prototype.prototype does't really make much sense, but as Function.prototype is implemented as a function object (which could possibly be used as a constructor), it should at least exists; objects which are created using Function.prototype as constructor would inherit its properties - but as there should be no reason to do something insane like this, setting it to null, undefined or an empty object is reasonable
Function.prototype.prototype.prototype em> undefined :如前所述, Function.prototype.prototype 应该是没有属性的元素( null , undefined 一个空对象)和definetely不是一个函数对象;因此,其原型属性应为未定义,或者在尝试访问时可能会发生错误
Function.prototype.prototype.prototype will in all likelyhood be undefined: as we have seen before, Function.prototype.prototype should be something without properties (null, undefined or an empty object) and definetely not a function object; therefore, its prototype property should be undefined or might even throw an error when trying to be accessed
希望这有帮助;)
这篇关于正确的原型链的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!