instanceof and the ECMA spec

Discussion in 'Javascript' started by pedz, May 21, 2011.

  1. pedz

    pedz Guest

    This message started off with the following question:

    >> In the ECMA spec, instanceof depends upon [[HasInstance]] but [[HasInstance]] is only defined for Function
    >> Objects. Why does:
    >>
    >> [ 1, 2, 3 ] instanceof Array
    >>
    >> not throw a TypeError as specified in step 6 of [1]?
    >>
    >> [1] http://es5.github.com/#x11.8.6
    >>
    >> How / where / why does Array get a [[HasInstance]] internal method? How is that method defined?


    I now see how/where/why it does. [ 1, 2, 3] is a call to new Array()
    and since Array is itself a function object (I guess), [[HasInstance]]
    is inherited from Array. Can someone give me an example of something,
    f, where typeof(f) would return "object" but f is not a function
    object? Or, to ask it another way, is "function object" essentially
    the same as "object" ?

    Confused...
     
    pedz, May 21, 2011
    #1
    1. Advertising

  2. On Sat, 21 May 2011 at 07:15:20, in comp.lang.javascript, pedz wrote:
    >This message started off with the following question:
    >
    >>> In the ECMA spec, instanceof depends upon [[HasInstance]] but
    >>>[[HasInstance]] is only defined for Function
    >>> Objects. Why does:
    >>>
    >>> [ 1, 2, 3 ] instanceof Array
    >>>
    >>> not throw a TypeError as specified in step 6 of [1]?
    >>>
    >>> [1] http://es5.github.com/#x11.8.6
    >>>
    >>> How / where / why does Array get a [[HasInstance]] internal method?
    >>>How is that method defined?

    >
    >I now see how/where/why it does. [ 1, 2, 3] is a call to new Array()
    >and since Array is itself a function object (I guess), [[HasInstance]]
    >is inherited from Array. Can someone give me an example of something,
    >f, where typeof(f) would return "object" but f is not a function
    >object? Or, to ask it another way, is "function object" essentially
    >the same as "object" ?
    >
    >Confused...


    To start at square 0 :
    ob instanceof C
    says true when C.prototype is one of the objects in ob's prototype
    chain. The expectation is that C is a function object that is used as a
    constructor.

    Note that if you implement inheritance in an untidy way then instanceof
    does not mean that ob could be used wherever a 'C' object can be used.
    I.e instanceof is not as precise as in other languages.

    [ 1, 2, 3 ] is a pseudo literal that creates an array that is "the
    result of creating a new object as if by the expression new Array()"
    (ECMA 262). Thus Array is a constructor function and [ 1, 2, 3 ] is an
    object that could have been constructed by it. So
    [ 1, 2, 3 ] instanceof Array
    will say true.

    To answer your other question, function objects are just one kind of
    object.
    new Object() and new Date()
    will produce different kinds of object, for instance.

    John
    --
    John Harris
     
    John G Harris, May 21, 2011
    #2
    1. Advertising

  3. pedz

    pedz Guest

    On May 21, 10:54 am, John G Harris <> wrote:
    > On Sat, 21 May 2011 at 07:15:20, in comp.lang.javascript, pedz wrote:
    > >This message started off with the following question:

    >
    > >>> In the ECMA spec, instanceof depends upon [[HasInstance]] but
    > >>>[[HasInstance]] is only defined for Function
    > >>> Objects.  Why does:

    >
    > >>> [ 1, 2, 3 ] instanceof Array

    >
    > >>> not throw a TypeError as specified in step 6 of [1]?

    >
    > >>> [1]http://es5.github.com/#x11.8.6

    >
    > >>> How / where / why does Array get a [[HasInstance]] internal method?
    > >>>How is that method defined?

    >
    > >I now see how/where/why it does.  [ 1, 2, 3] is a call to new Array()
    > >and since Array is itself a function object (I guess), [[HasInstance]]
    > >is inherited from Array.  Can someone give me an example of something,
    > >f, where typeof(f) would return "object" but f is not a function
    > >object?  Or, to ask it another way, is "function object" essentially
    > >the same as "object" ?

    >
    > >Confused...

    >
    > To start at square 0 :
    >    ob instanceof C
    > says true when C.prototype is one of the objects in ob's prototype
    > chain. The expectation is that C is a function object that is used as a
    > constructor.
    >
    > Note that if you implement inheritance in an untidy way then instanceof
    > does not mean that ob could be used wherever a 'C' object can be used.
    > I.e instanceof is not as precise as in other languages.
    >
    > [ 1, 2, 3 ] is a pseudo literal that creates an array that is "the
    > result of creating a new object as if by the expression new Array()"
    > (ECMA 262). Thus Array is a constructor function and [ 1, 2, 3 ] is an
    > object that could have been constructed by it. So
    >    [ 1, 2, 3 ] instanceof Array
    > will say true.
    >
    > To answer your other question, function objects are just one kind of
    > object.
    >    new Object()  and  new Date()
    > will produce different kinds of object, for instance.
    >
    >    John
    > --
    > John Harris


    Hi John,

    I think I understood all of what you just wrote already. But its good
    to make sure.

    [[HasInstance]] is only defined (according to the spec) for function
    objects (and a special case that is the result of bind). Thus new
    Object() and new Date() must also create function objects since they
    have a [[HasInstance]] method.

    I suppose a better way to ask my question is can you give me an
    example that passes step 5 but fails step 6 of 11.8.6?

    Note: as you said, you can do really weird things. That isn't really
    my quest either. I'm really just trying to understand. If I did new
    Object(), I don't think of that as a function object but yet, as I
    read the spec, it must be. I'm sure I'm missing something in the spec
    and I'm trying to see what I'm missing.

    Thanks
    pedz
     
    pedz, May 21, 2011
    #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. PJ
    Replies:
    0
    Views:
    409
  2. Replies:
    21
    Views:
    22,230
  3. Michael Rasmussen

    ECMA-372 is stepping in on C++

    Michael Rasmussen, Jan 20, 2006, in forum: C++
    Replies:
    12
    Views:
    644
    Chris Hills
    Jan 22, 2006
  4. Andrew Chen
    Replies:
    1
    Views:
    227
    David Chelimsky
    Mar 25, 2008
  5. Dmitry A. Soshnikov
    Replies:
    12
    Views:
    204
    Dr J R Stockton
    Apr 30, 2010
Loading...

Share This Page