how to tell if an object is an instance of a class or one of its subclasses

113 views
Skip to first unread message

John Munro

unread,
May 24, 2013, 5:14:40 PM5/24/13
to closure-lib...@googlegroups.com
Is there a way to check if an object is an instance of a class or one of its subclasses?  goog.typeOf() only gets me the exact class name.

For example if a storage variable contains an instance of goog.storage.ExpiringStorage or one of its subclasses then I want to use the three-argument constructor, but if it isn't then I need to use the two-argument constructor.

Nathan Naze

unread,
May 24, 2013, 5:25:09 PM5/24/13
to closure-library
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/instanceof

You'll have to special case each of the subclasses.

It may be that you want to bind together a factory function that takes
zero parameters and pass that along so your code doesn't have to know
or care what constructor it's using.

Nathan
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "Closure Library Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to closure-library-d...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

John Munro

unread,
May 24, 2013, 5:32:05 PM5/24/13
to closure-lib...@googlegroups.com
Is there a way to find out how many parameters a function expects?  If there is then I could just test for that instead...
> email to closure-library-discuss+unsub...@googlegroups.com.

John Lenz

unread,
May 24, 2013, 6:45:32 PM5/24/13
to closure-library
yes, and yes

For the first: 
It isn't clear what you want but let me give two solutions:

If a variable "x" is actually an instance of a subclass of goog.storage.ExpiringStorage:

   x instanceof goog.storage.ExpiringStorage

If a variable "x" is an alias of the constructor (you are passing the constructor to your function) you can do:
  
  for (var tmp = x, found = false; tmp != null; tmp = tmp.superClass_) {
    if (tmp == goog.storage.ExpiringStorage) {
       found = true; 
       break;
    }
  }

What you propose is a bit fragile, as you are relying subclass constructor having a known set of parameters.  One alternative is to instead pass a "factory" function that take a know set of parameters.  As you would be writing this yourself, you can be certain no one is passing in a incompatible constructor.

For the second:

Assuming x is a function, then "x.length" returns the number of declared parameters, but you don't really want to use this in ADVANCED compiled code as the compiler can rewrite the function definition to remove unused or inline constant parameters.


To unsubscribe from this group and stop receiving emails from it, send an email to closure-library-d...@googlegroups.com.

John Munro

unread,
May 28, 2013, 9:18:48 AM5/28/13
to closure-lib...@googlegroups.com
Thanks, instanceof was what I was looking for
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages