5

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

Community
  • 1
  • 1
Ekim
  • 949
  • 1
  • 8
  • 9

4 Answers4

6

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.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • 1
    Beat me to it ... however, I dislike the use of "references" when talking about JavaScript (and here is my suggestion of how to read/phrase the first sentence): You didn't create [new] objects, you assigned the *function* Object [which is a constructor] to the variables. –  Aug 10 '11 at 21:29
  • That's a good way to say it as well, definitely. The key being that you can create Objects that are not Functions, but not by assigning the Object Function to a variable. :) – g.d.d.c Aug 10 '11 at 21:33
  • yes, yes ... but ... IS there Object which is not a function. Note the careful phrasing and syntax. Capitalized "Object" and no article "an". I do mean the "Object" entity. Is there one (Object) which is not a function!? The question was not about creating objects. – Ekim Aug 10 '11 at 22:03
  • @ekim - When the JS Interpreter loads Object is the Object Function (which is the New Object Constructor). You can call `Object = {}` to turn it into an actual Object Literal, but this eliminates the ability to call `new Object()` as you've overwritten the previous signature. I ... am not certain I grasp why you are asking in the way you are asking. – g.d.d.c Aug 10 '11 at 22:25
  • but `x`, `y` and `z` ARE objects - (albeit the same one) - they are the (one and only) Object object which is an object of type function or simply a function. – Ekim Aug 11 '11 at 00:33
  • I am being somewhat facetiously rhetorical ... however, JavaScript is a functional programming language where everything is a function as well as an object. There are subtleties of nuance regarding objects and functions and their mechanisms of manifestation. Object, new Object(), Object(), Function, new Function(), Function() The question is very simple and the answer is equally simple - YES. I was curious to see what the perceptions are of the very statement at face value. Intriguing that no one is answering directly! – Ekim Aug 11 '11 at 00:48
2

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.

Shad
  • 15,134
  • 2
  • 22
  • 34
  • exactly - but still the question remains - Is the answer a "YES"? – Ekim Aug 10 '11 at 22:04
  • @ekim Objects and functions are the two root, non-primitive elements of Javascript https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals – Shad Aug 11 '11 at 02:31
  • https://developer.mozilla.org/en/JavaScript/Guide/Functions and https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects describes construct fundamentals. – Ekim Aug 11 '11 at 03:48
  • The given reference, https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals, describes tokens of JavaScript - https://developer.mozilla.org/en/JavaScript/Guide/Predefined_Core_Objects defines Core_Objects (primitives) except Object is conspicuously absent as enumerated here https://developer.mozilla.org/en/JavaScript/Reference#Standard_global_objects_(by_category) . – Ekim Aug 11 '11 at 04:05
1

Any function can be used as a constructor to create an object by using the new operator before the function name in JavaScript. The resulting object will not be a Function.

There is also a circular relationship between Object and Function that can be tested with:

Object instanceof Function // true
Function instanceof Object // true

And {} and Object are not the same, but {} and new Object() are.

function foo() {}
foo instanceof Function // true
foo instanceof Object // true

var bar = new foo();
bar instanceof Function // false
bar instanceof Object // true

var baz = {};
baz instanceof Function; // false
baz instanceof Object; // true
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • There is no "circular relationship". Object and Function are both functions, they both inherit from Function.prototype, which in turn inherits from Object.prototype (so they are both functions, and all javascript functions are also objects). The *instanceOf* operator just checks the prototype chain, it really doesn't tell you much. – RobG Aug 10 '11 at 23:47
  • so ... the simple answer is ... is that a YES?, EVERY Object (even though there is only one that I know of but there is this one) IS a function! – Ekim Aug 11 '11 at 01:02
  • "Any function .. used as a constructor to create an object by using the `new` operator ... will not be a `Function`" is not true, proof: `javascript:function f(){return function(){}};alert(new f)` – Ekim Aug 20 '11 at 14:38
  • Actually there is a recursive circular relationship and it is well- defined based on the constructor mechanism regarding objects and functions of which Function and Object are both representatives. Consider, `javascript:with(Object)alert([Object,constructor,prototype.constructor ])` details are provided in http://chat.stackoverflow.com/rooms/2245/discussion-between-ekim-and-felix-kling – Ekim Aug 21 '11 at 14:48
0

Here is a surefire way to check the type of anything in js.

note: you should send in something other than window...

try it out here...

            (function (self) {
                var values = [
                          'Function',
                          'Object',
                          'Array',
                          'String',
                          'Number',
                          'Date',
                          'RegExp',
                          'Boolean',
                          'Null',
                          'Error'
                     ];

                for (var i in values) if (values.hasOwnProperty(i)) {
                     var value = values[i];

                     self['is' + value] = (function (v) {
                          return function (o) {
                                 var r = '';

                                 try {
                                     r = (o === null) ?
                                              'Null' :
                                              Object.prototype.toString.call(o).replace(/^\[object\s(\w+)\]$/, '$1');
                                 } catch (e) {
                                     r = 'Undefined';
                                 }

                                 return !!(r === v);
                          };
                     })(value);
                }
           })(window);

           alert(isFunction(Object));
shawndumas
  • 1,413
  • 15
  • 17