Is every JavaScript Object a function?

后端 未结 4 839
花落未央
花落未央 2020-12-10 18:31

Is there a JavaScript Object that is not a function?

javascript: x=y=z=Object; alert([window.navigator.userAgent,x,y,z].join(\"\\n\\n\"))

(

4条回答
  •  爱一瞬间的悲伤
    2020-12-10 19:17

    You didn't create objects, you created references to the Object function. If you wanted those to be objects you could do this:

    x = y = z = {}
    

    Then alert(x) will return object [Object].

    To (hopefully) encompass the comments - by default Object is a Function which constructs Objects. If you reassign the name Object (Firefox at least seems to allow me to, haven't tested all browsers) then Object will no longer be a Function, it will be whatever you assigned to it. So then, the answer is "no", Object is not always a Function, but should be unless it has been explicitly re-declared. According to Firebug:

    >>> Object
    Object()
    >>> Object = {}
    Object {}
    >>> Object
    Object {}
    

    Seemingly it can be reassigned. I cannot vouch for what kind of impacts that would have, if any.

提交回复
热议问题