Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is every JavaScript Object a function?

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"))

(There was a comment that x,y,z are merely references in which case Object is also merely a reference to function Object(){ ... } because Object's value is assigned to x and they are the "same". As "proof"

javascript:x=Object;x.p=43;alert([x==Object,x===Object,x.p,Object.p])

displays

true,true,43,43

Given function Thing(){} does x=new Thing() make x an object or a reference to one? What about new Thing() and Thing? Or y in y=x=new Thing() or y=x=Thing? What if Thing=function(){}? The distinction is moot. "Everything" (or is it?) is called-by-reference but call-by-name can be coerced by evaluating strings. So ...)

javascript:
    void function(x,y,z){
        alert(  [window.navigator.userAgent,x,y,z].join("\n\n") )
    }(Object,Object,Object)

or

javascript:
    void function(x){  (function (y){  (function (z){
             alert(  [window.navigator.userAgent,x,y,z].join("\n\n") )
         })(y) })(x) }(Object)

(well not quite moot - the function's values must be coerced using (...) or void. The nuances of (...) are subtle:

javascript:       /* 43.p gives a runtime error but not ... */
    alert([ (43).p=34, 43["q"]=17, (x=43).z="hmmm" ]); 
    alert([ 43["p"], (43).z, x.p, x["z"], x]);

displays 34,17,hmmm and ,,,,43

)

or even an array of Objects

javascript:alert([window.navigator.userAgent,Object,Object,Object].join("\n\n"))

gives:

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3

function Object() { [native code] }

function Object() { [native code] }

function Object() { [native code] }

There are many objects that are not Object.


As pointed out in one of the answers, Object may not be itself IF it is modified.
Danger! Danger! Will Robinson!

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

references

  • Object and Function are quite confusing
  • Difference between a constructor and an Object
  • Is Function really an Object
  • Is JavaScript function a "function" or an "object" or both?
  • Every Object is a function and every function is Object - Which is Correct?
  • Why in JavaScript is a function considered both a constructor and an object?
like image 479
Ekim Avatar asked Aug 10 '11 21:08

Ekim


2 Answers

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.

like image 124
g.d.d.c Avatar answered Oct 07 '22 14:10

g.d.d.c


You are assigning the Object constructor to the vars x, y and z. If you instead say x=new Object(), you will no longer see them referred to as functions.

like image 20
Shad Avatar answered Oct 07 '22 13:10

Shad