开发者

why **(Object.__proto__ instanceof Function)** === false?

why does开发者_运维百科 Object._proto_ instanceof Function gives me false?

alert(Object.__proto__ ); //  clearly Object.__proto__ is a function right?
alert(typeof Object.__proto__); // clearly Object.__proto__ is a function right?
alert(Object.__proto__ instanceof Function); // !


Not all functions are created via the Function constructor. instanceof checks specifically to see if the given item was created by that specific function.

You get a similar effect in browser environments when dealing with multiple windows. I mean, if you have function foo in window A:

function foo(arg) {
    if (arg instanceof Array) {
        // It's an array, do something
    }
}

...and you have code in another window B that calls it:

opener.foo([]);

...then you'd expect foo to realize that arg is an array, right? But it doesn't, because although arg is an array, it wasn't created by the Array constructor in the window that foo is in.

More about figuring out what things are here: Say what?

If you're fascinated by this stuff (as you seem to be), there's nothing quite like reading the specification. Yes, the prose is...dry...and the terminology is....dense...but it gets more and more interesting as you learn more and more about the underlying workings.


Off-topic: Beware that __proto__ is non-standard and not supported by all JavaScript implementations.


To end with the mystery:

What exactly is Object.__proto__?

  • It is simply a reference to the Function.prototype object.

    Object.__proto__ === Function.prototype; // true
    

The Object constructor as almost all of the built-in and user defined functions, inherit from Function.prototype.

This object (Function.prototype) is described in the specification to be a Function object, but obviously, an object can't inherit from itself, and that's why it inherits from Object.protoype instead.

Your test:

Object.__proto__ instanceof Function;   // false, which is equivalent to:
Function.prototype instanceof Function; // false

Is simply telling us that the Function.prototype object is not on the prototype chain of Function.prototype itself:

Function.prototype.isPrototypeOf(Function.prototype); // false, equivalent to:
Object.prototype.isPrototypeOf.call(Function.prototype, Function.prototype);

As I told before, in the specification this object described with the following characteristics, if you are interested:

  • It is a function object (implements the [[Call]] internal method).

    typeof Function.prototype; // "function"
    
  • The value of its [[Class]] internal property is "Function".

    Object.prototype.toString.call(Function.prototype); // "[object Function]"
    
  • The value of its [[Prototype]] internal property points to Object.prototype (as you know now).

    Object.prototype.isPrototypeOf(Function.prototype); // true
    
  • Can be called with any number of arguments.

  • It always returns the undefined value.

  • Its length property is 0.

  • The initial value of its [[Extensible]] internal property is true.


alert(Object.__proto__ ); //  clearly Object.__proto__ is a function right?
alert(typeof Object.__proto__); // clearly Object.__proto__ is a function right?
alert(Object.__proto__ instanceof Function); // !

Exactly, it is a function. However, it is not a function object. That's why instanceof returns false. You can read all about instanceof here.


the image in this post will help you understand the Function vs Object relation

JavaScript Object Layout

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜