Object and Function are quite confusing

前端 未结 7 1768
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 03:47
Object instanceof Object
true
Object instanceof Function
true
Function instanceof Object
true
Function instanceof Function
true

so if Function is a

相关标签:
7条回答
  • 2020-12-10 04:13

    instanceof operator indicates if the first argument is of the given type. That is saying

     A instanceof B
    

    returns true if A is an instance of the type B.

    The == and === operators are comparison operators on the other hand. They compare values for equality.

    For instance, you can say jack instanceof Boy is true but would you say that jack == boy? No.

    0 讨论(0)
  • 2020-12-10 04:16

    Everything is an Object in JavaScript because JavaScript is an object-oriented language. Function is an instance of Object because everything is an instance of Object. Simple enough. However, objects that initialize other objects (constructors) are also Functions in JavaScript, so it would make sense for Object to also be a Function.

    Think about this:

    var obj = new Object();
    

    Object in this case is used as a Function, is it not? So while, in theory, Object should be the lowest-level object in the language, JavaScript cannot function without Functions (pun!), so you need both to be at the same level. Object needs to be an instance of Function because it's a constructor and it needs to create more instances of itself.

    function FooBar() {}
    

    The FooBar class above is an instance of both Object and Function, because it's both. The same logic applies to the built-in Object and Function objects; they're instances of both.

    Phew, confusing. Did that make any sense?

    0 讨论(0)
  • 2020-12-10 04:18

    From JavaScript Prototypal Inheritance:

    Quite everything, in JavaScript, inherits from Object. We could say that Object is the super class, or better, the super constructor, of every variable and that everything is an instanceof Object. The Object constructor is a Function, but a Function is an instanceof Object. This means that these assertions are always true:

    (Object instanceof Function) === (Function instanceof Object)

    Above example is true because Object is a constructor, and a constructor in JavaScript is always a Function. At the same time, every Function has its own prototype, and a prototype always starts its inheritance from Object.prototype. The Function constructor, is a Function itself, and the function prototype is a function(){};

    (Function.prototype instanceof Object) === (function(){} instanceof Object)

    0 讨论(0)
  • 2020-12-10 04:21

    There is no such thing as classes in Javascript. The instanceof operator is called on functions.

    Object is a constructor function for "Object objects" (yes, this is the official term), and Function is the constructor function for "Function objects".

    So, when you call Function instanceof Object, it returns true because Function is a function, and thus an object, etc. This does not mean that the types are the same, because Object and Function have different prototypes:

    Object.prototype
       ^
       | inherits from
       |                instance
    Function.prototype  <-------  Object, Function
    
    0 讨论(0)
  • 2020-12-10 04:22

    Suppose:

    <---- : links to (via .__proto__)

    <-- : has inherited property of (via .)

    Then:

    Function.prototype <---- {Object, Function}

    Object <---- Function.prototype

    Object.prototype <-- Object

    console.log(Object.prototype.constructor === Object); // true!
    

    Object.prototype does not link to anything; it doesn't have __proto__ property.

    0 讨论(0)
  • 2020-12-10 04:27
     javascript:alert([ window.navigator.userAgent, Object ].join("\n\n") )
    

    displays

    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]
    }
    

    reference: Is every JavaScript Object a function?

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