Is every JavaScript Object a function?

后端 未结 4 800
花落未央
花落未央 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 18:56

    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));
    
    0 讨论(0)
  • 2020-12-10 18:58

    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
    
    0 讨论(0)
  • 2020-12-10 19:00

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题