instanceof and the ECMA spec

P

pedz

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...
 
J

John G Harris

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
 
P

pedz

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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top