Correct prototype chain for Function

前端 未结 4 985
-上瘾入骨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条回答
  •  Happy的楠姐
    2020-12-05 04:20

    I know this post is kind of old, but I've been searching the net for information on this subject and figured I'd post what I found. The prototype property is for constructor functions. It allows you to assign the prototype object of objects you will create with the new keyword.

    Every object in JavaScript has a prototype object, but many implementations don't give you direct access to it or allow you to set it after object creation. In FireFox, you can access this object via the "__proto__" property.

    Below I have a version of your code using the "__proto__" property. The section on the Function prototype chain matches what you thought it should have been.

    function nl(z) { document.write(z + "
    "); } x = {}; nl(x["__proto__"]); nl(x["__proto__"] === Object.prototype); nl(""); nl(nl.prototype.constructor); nl(nl["__proto__"].constructor); nl(nl["__proto__"] === nl.prototype); nl(""); nl(nl["__proto__"]); nl(nl["__proto__"] === Function.prototype); nl(nl["__proto__"]["__proto__"] === Object.prototype); nl(nl["__proto__"]["__proto__"]["__proto__"]); nl(""); nl(Function["__proto__"]); nl(Function["__proto__"]["__proto__"]); nl(Function["__proto__"]["__proto__"] === Object.prototype); nl(Function["__proto__"]["__proto__"]["__proto__"]);

    Output in FireFox is:

    [object Object]
    true
    
    function nl(z) { document.write(z + "
    "); }
    function Function() { [native code] }
    false
    
    function () { }
    true
    true
    null
    
    function () { }
    [object Object]
    true
    null
    

提交回复
热议问题