Forum Discussion

viverosm's avatar
viverosm
New Contributor
11 years ago

instance of operator for different Checkpoint types

Hello,



I have an array of objects used in checkpoints and wanted to iterate over it and do different things based on the type of object (if it's a Table, do A, if it's a Region, do B). I am using JavaScript and I tried the code below but I got an error saying Table is undefined. Is there another way to use the instance of operator to distinguish between Region objects and Table objects:



for (i = 3; i < checks.length; i++) {


  if (checks instanceof Table) {

    ... A ...

  } else {

    ... B ...

  }


 


  • Hi Michael, as per the documentation here, instanceof is a native JScript operator that tests whether an object has in its prototype chain the prototype property of a constructor.


    Therefore, it will only work if the Table and Region Objects are native JScript Objects, which I would doubt.


     


    Also, how are you able to get an Array of Table and Region checkpoints, I could not find any way to achieve this.


     


    It may be that the error saying Table is undefined is being raised as Table does not exist as an Object.


     


    Regards,


    Phil Baird

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Hi Michael, as per the documentation here, instanceof is a native JScript operator that tests whether an object has in its prototype chain the prototype property of a constructor.


    Therefore, it will only work if the Table and Region Objects are native JScript Objects, which I would doubt.


     


    Also, how are you able to get an Array of Table and Region checkpoints, I could not find any way to achieve this.


     


    It may be that the error saying Table is undefined is being raised as Table does not exist as an Object.


     


    Regards,


    Phil Baird

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Hi Michael, a way in which you could use instanceof is is to wrap each Object in a specific JScript Constructer, as such:

     


    function RegionCheckPointWrapper( region ) {


      this.Region = region;


    }


     


    function TableCheckPointWrapper( table ) {


      this.Table = table;


    }


     


    You could now use instanceof on the Wrapper:


     


    function testCheckPointWrappers() {


      var region =  new RegionCheckPointWrapper( "Should be a Region Object" );


      var table = new TableCheckPointWrapper( "Should be a Table Object" );


      


      Log.Message( region instanceof RegionCheckPointWrapper );


      Log.Message( region instanceof TableCheckPointWrapper );


      Log.Message( table instanceof RegionCheckPointWrapper );


      Log.Message( table instanceof TableCheckPointWrapper );


    }


     


    The downside of this is you need to specifically wrap an Object in the appropriate Constructor, which you may not want to have to do.


     


    Regards,


    Phil Baird

  • viverosm's avatar
    viverosm
    New Contributor
    Hey Phil,



    Thanks for the replies. It makes sense now why instance of does not work since Regions and Tables are not native JScript objects and a constructor was never used to create them. Wrapping each object in a constructor is a creative work-around too. I ended up just ordering the objects in the array in a specific way so that they could be iterated over in groups. The code is a bit messier / longer and not that maintainable but gets the job done for now and is less work than calling that constructor for each Region / Table in the array.



    Below is how I defined an array of Region and Table objects (not checkpoints), assuming these objects have already been created in the Regions and Tables data stores of the project:


     


    var T3_CHECKS3 = [Regions.T3_Comm, Tables.T3_Sigs, Tables.T3_Sigs2, Regions.T3_Dialog1, Regions.T3_Dialog2];



    I then do the checkpoints by making calls like T3_CHECK3[0].Check(ValidRegion) and T3_CHECKS[1].Check().



    Michael