1. #1
    Sencha User
    Join Date
    Mar 2010
    Posts
    25
    Vote Rating
    1
    BigSeanDawg is on a distinguished road

      0  

    Default Answered: Quick/Preferred way to check object is instance of class, including super classes?

    Answered: Quick/Preferred way to check object is instance of class, including super classes?


    Is there any way for me to check that an object extends a certain class? I looked through docs and these forums, but couldn't find an answer (even though it seems like it might be a common question).

    My use case is this:

    I have a method that supports passing in either an instance of a Model (aka a Record), or an HTMLElement. Most of the method can work with either object, but I need to do one special thing if you gave me a record. Since my Model classes extend Ext.data.Model (obviously), I can't check the class name. So what's the best way to determine if the object is a model or not?

    My preferred method would be:
    Code:
    var isModel = Ext.isInstance(obj, 'Ext.data.Model')

  2. Code:
    var isModel = obj instanceof Ext.data.Model;

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,330
    Answers
    3541
    Vote Rating
    847
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Code:
    var isModel = obj instanceof Ext.data.Model;
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User
    Join Date
    Mar 2010
    Posts
    25
    Vote Rating
    1
    BigSeanDawg is on a distinguished road

      0  

    Default


    Thanks for another answer to an extremely easy question. For some reason I wasn't thinking about these as being part of a true class hierarchy. Thanks!

Thread Participants: 1

Tags for this Thread