Correct prototype chain for Function

前端 未结 4 993
-上瘾入骨i
-上瘾入骨i 2020-12-05 03:38

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

function nl(x) { document.write(x + \"
\"); } nl(Functio
4条回答
  •  悲哀的现实
    2020-12-05 04:16

    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:

    • Function is the constructor of function objects

    • 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 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 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

    Hope this helps ;)

提交回复
热议问题