Function instanceof Object and vice-versa ?

Discussion in 'Javascript' started by System Administrator, Mar 10, 2007.

  1. Hi:

    Can someone explain why BOTH of the following are true ?

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


    (Either one or the other should be true, not both, right ?)

    Best regards,
    --j
     
    System Administrator, Mar 10, 2007
    #1
    1. Advertising

  2. System Administrator

    VK Guest

    On Mar 10, 11:07 pm, System Administrator
    <> wrote:
    > Hi:
    >
    > Can someone explain why BOTH of the following are true ?
    >
    > Function instanceof Object //true
    > Object instanceof Function //true
    >
    > (Either one or the other should be true, not both, right ?)


    Wrong. This is prototype based language here, not a class based.
    a instanceof b
    check is a "syntax beautifying" for
    b.prototype.isPrototypeOf(a)
    While "making JavaScript out of LiveScript" one of requests to Brendan
    Eich was to make the syntax closer to Java - because Java was cool
    everything else lame :) at that time.

    Now analyze the result with the actual meaning of instanceof in
    javascript in mind. Overall until
    http://blogs.msdn.com/ericlippert/archive/2003/11/06/53352.aspx
    is fully read and understood you will keep getting a lot "puzzles"
    like that one.
     
    VK, Mar 10, 2007
    #2
    1. Advertising

  3. System Administrator

    RobG Guest

    On Mar 11, 6:07 am, System Administrator
    <> wrote:
    > Hi:
    >
    > Can someone explain why BOTH of the following are true ?
    >
    > Function instanceof Object //true
    > Object instanceof Function //true


    Function and Object are both constructors, and therefore functions.
    And all functions are also objects.


    So:

    function x(){};
    x instance of Object // true
    x instance of Function // true

    y = {};
    y instanceof Object // true
    y instanceof Function // false

    --
    Rob
     
    RobG, Mar 10, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. carmen
    Replies:
    4
    Views:
    30,753
    ersecchio
    Jan 12, 2010
  2. Sam Hwang

    Java Object to XML and vice versa

    Sam Hwang, Apr 24, 2005, in forum: Java
    Replies:
    5
    Views:
    1,024
    iksrazal
    Apr 25, 2005
  3. JyotiC

    xml to object and vice versa

    JyotiC, Jul 23, 2008, in forum: Java
    Replies:
    10
    Views:
    647
    JyotiC
    Jul 30, 2008
  4. Tamer Higazi
    Replies:
    1
    Views:
    182
    Steven D'Aprano
    Jan 22, 2012
  5. Jimmy
    Replies:
    3
    Views:
    459
    Henry
    Aug 16, 2007
Loading...

Share This Page