On 12/24/10, Michael Haufe (TNO) <t...@thenewobjective.com> wrote:
> On Dec 24, 6:43 pm, Garrett Smith <dhtmlkitc...@gmail.com> wrote:
>> The client of the API gets an interface object that has properties and
>> methods so instanceof and constructor shouldn't matter.
>
> instanceof is important for an interface  consumer to be certain that
> they are using the proper object:
>
I disagree.

> function draw(group){
>     if(group instanceof Group){

If the program is written so that group then it won't matter. The
program can be safe enough to not have to worry about that. Example:

var group = createGroup();
draw(group);

The program would be clear what `group can do at that point. The
constructor or prototype wouldn't matter; what matters at that point
is what `group` can do.

But if you're not sure at any point, then sure, you can use
inferential tests like this:

>     if(typeof group == "object" && typeof group.method1 == "function"
>        && typeof group.method2 == "function"
>        && typeof group.method3 == "function"){
>          //well, it looks like a group....

I don't like to see all that into an inline block. It is much less
cluttered if the typechecking is moved into a separate function, such
as:

if(Group.isGroupLike(group)) {
>         group.method1();
>         group.method2();
>         group.method3(); //oops, its not the droid you're looking for
>     } else {
>         throw new Error("Shape Group Expected");
>     }
> }
>
Better yet just get rid of the typechecking there.

But, when I want to use instanceof, which is rarely, it is inside scope.

For example, in domunit, there is a catch block that catches what
might be any of: A thrown custom error (EarlyExit or Wait), or a
runtime test error. EarlyExit and Wait are in the same scope so they
can't be coming from another frame and instanceof can work there.
-- 
Garrett

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to