mng0
2008-Feb-24 21:30 UTC
How do I check if a variable is an instance of a specific object?
var myDog = new Dog(); How do I simply check if myDog is a instance of the Dog class? Why I''m asking is because of this. I have an array with Dogs and Cats objects. And in one case I only want to get all the Dogs from the array. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Antonio CS
2008-Feb-24 21:36 UTC
Re: How do I check if a variable is an instance of a specific object?
I not have a method in both classes that returns what they are? On Sun, Feb 24, 2008 at 9:30 PM, mng0 <ggustav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > var myDog = new Dog(); > > How do I simply check if myDog is a instance of the Dog class? > > Why I''m asking is because of this. I have an array with Dogs and Cats > objects. And in one case I only want to get all the Dogs from the > array. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
kangax
2008-Feb-24 21:38 UTC
Re: How do I check if a variable is an instance of a specific object?
var myDog = new Dog(); myDog instanceof Dog; // => true myDog instanceof Cat; // => false http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Operators:Special_Operators:instanceof_Operator - kangax On Feb 24, 4:30 pm, mng0 <ggus...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> var myDog = new Dog(); > > How do I simply check if myDog is a instance of the Dog class? > > Why I''m asking is because of this. I have an array with Dogs and Cats > objects. And in one case I only want to get all the Dogs from the > array.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
mng0
2008-Feb-24 21:45 UTC
Re: How do I check if a variable is an instance of a specific object?
Antonio CS, yeah that would be one way to do it, but I liked kangax''s solution better. Thanks both of you! Less code for the win. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---