isaOK: instanceof vs. isPrototypeOf

16 views
Skip to first unread message

Marvin Humphrey

unread,
Aug 3, 2007, 12:36:27 AM8/3/07
to jsan-a...@googlegroups.com
Greets,

In Test.More, why use the isPrototypeOf() method instead of the
instanceof operator? Safari doesn't have isPrototypeOf, so my tests
isaOK tests there fail. However, I can achieve the same end with the
following:

isaOK(customErrorObject, "Error"); // breaks on Safari
ok(customErrorObject instanceof Error, "Yup, it's an Error");

The JavaScript 1.5 documentation indicates that instanceof is
specifically intended for purposes like this.

http://xrl.us/3qyq (Link to developer.mozilla.org)

Patch file at:

http://www.rectangular.com/junk/Test.Simple/isaOK.patch

This passes on IE6, FF2mac FF2win, and Safari2mac:

http://www.rectangular.com/junk/Test.Simple/tests/?
file=more.js;verbose=1

I see reference to the same Safari bug number in other spots when I
ack the source, but this is all I have time for tonight and seems
like a good start.

Marvin Humphrey
Rectangular Research
http://www.rectangular.com/


David E. Wheeler

unread,
Aug 3, 2007, 1:05:11 AM8/3/07
to jsan-a...@googlegroups.com
On Aug 2, 2007, at 21:36, Marvin Humphrey wrote:

> In Test.More, why use the isPrototypeOf() method instead of the
> instanceof operator? Safari doesn't have isPrototypeOf, so my tests
> isaOK tests there fail. However, I can achieve the same end with the
> following:
>
> isaOK(customErrorObject, "Error"); // breaks on Safari
> ok(customErrorObject instanceof Error, "Yup, it's an Error");
>
> The JavaScript 1.5 documentation indicates that instanceof is
> specifically intended for purposes like this.
>
> http://xrl.us/3qyq (Link to developer.mozilla.org)
>
> Patch file at:
>
> http://www.rectangular.com/junk/Test.Simple/isaOK.patch
>
> This passes on IE6, FF2mac FF2win, and Safari2mac:
>
> http://www.rectangular.com/junk/Test.Simple/tests/?
> file=more.js;verbose=1

Well, yeah, because you deleted some tests, or so it appears to me.

As I've said, I'm a long way from this code at the moment, but from
your patch it *looks* like inheritance is lost. IIRC, instanceof is
like `ref $obj eq 'Foo'` in Perl, rather than `$obj.isa('Foo')`.

Am I missing something?

Best,

David

Marvin Humphrey

unread,
Aug 3, 2007, 1:13:14 AM8/3/07
to jsan-a...@googlegroups.com

On Aug 2, 2007, at 10:05 PM, David E. Wheeler wrote:

> Well, yeah, because you deleted some tests, or so it appears to me.

Take a closer look. :)

-if (safari) skip("http://bugzilla.opendarwin.org/show_bug.cgi?
id=3537", 1);
-else isaOK(Test.Builder.Test, "Test.Builder");
+isaOK(Test.Builder.Test, "Test.Builder");
+isaOK(Test.Builder.Test, "Object")

-if (Object.isPrototypeOf) isaOK(Test.Builder.Test, "Object")
-else skip("Cannot check inheritance without isPrototypeOf()", 1);
-


I didn't delete the tests, I deleted the conditional skipping of tests.

> As I've said, I'm a long way from this code at the moment, but from
> your patch it *looks* like inheritance is lost. IIRC, instanceof is
> like `ref $obj eq 'Foo'` in Perl, rather than `$obj.isa('Foo')`.
>
> Am I missing something?

It really is like $obj->isa('Foo'). Those tests above pass.

I specifically needed this functionality for testing an Error hierarchy:

Error => MyError => MyIdiotUserErrorWithSafeMessage

David E. Wheeler

unread,
Aug 3, 2007, 8:02:33 PM8/3/07
to jsan-a...@googlegroups.com
On Aug 2, 2007, at 22:13, Marvin Humphrey wrote:

> I didn't delete the tests, I deleted the conditional skipping of
> tests.

Bah, sorry, my mistake.

> It really is like $obj->isa('Foo'). Those tests above pass.
>
> I specifically needed this functionality for testing an Error
> hierarchy:
>
> Error => MyError => MyIdiotUserErrorWithSafeMessage

Huh. There must have been *some* reason I did it this way. I just
don't now what it was now, of course.

Patch applied (or will be, once I can auth the svn repo again). Thanks!

David

Marvin Humphrey

unread,
Aug 21, 2007, 8:19:53 PM8/21/07
to jsan-a...@googlegroups.com

On Aug 3, 2007, at 5:02 PM, David E. Wheeler wrote:

> Patch applied (or will be, once I can auth the svn repo again).
> Thanks!

(*nudge*)

:)

TIA,

David E. Wheeler

unread,
Aug 21, 2007, 11:41:32 PM8/21/07
to jsan-a...@googlegroups.com
On Aug 21, 2007, at 17:19, Marvin Humphrey wrote:

>> Patch applied (or will be, once I can auth the svn repo again).
>> Thanks!
>
> (*nudge*)
>
> :)

Bah. Pinging jcap again.

jcap: Can you reset my svn password?

Thanks,

David

David E. Wheeler

unread,
Aug 21, 2007, 11:52:20 PM8/21/07
to jsan-a...@googlegroups.com
On Aug 21, 2007, at 20:41, David E. Wheeler wrote:

>> (*nudge*)
>>
>> :)
>
> Bah. Pinging jcap again.
>
> jcap: Can you reset my svn password?

Got him on IRC; it's committed.

Best,

David

Reply all
Reply to author
Forward
0 new messages