Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > instanceof and the ECMA spec

Reply
Thread Tools

instanceof and the ECMA spec

 
 
pedz
Guest
Posts: n/a
 
      05-21-2011
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...
 
Reply With Quote
 
 
 
 
John G Harris
Guest
Posts: n/a
 
      05-21-2011
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
 
Reply With Quote
 
 
 
 
pedz
Guest
Posts: n/a
 
      05-21-2011
On May 21, 10:54*am, John G Harris <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ECMA-262-5 in detail. Chapter 1. Properties and Property Descriptors. Dmitry A. Soshnikov Javascript 12 04-30-2010 08:40 PM
How to control order of spec execution in "spec specs/* " ? Andrew Chen Ruby 1 03-25-2008 12:36 PM
instanceof NOT (always) bad? The instanceof myth. dmx_dawg@hotmail.com Java 21 07-20-2006 07:06 PM
ECMA-372 is stepping in on C++ Michael Rasmussen C++ 12 01-22-2006 02:14 PM
ECMA/IE/DNS/IIS/asp.net, I have no idea PJ ASP .Net 0 08-21-2003 09:44 AM



Advertisments