Referring to parent object in JSCript - InfoPath Dev
in

InfoPath Dev

Referring to parent object in JSCript

Last post 03-10-2008 09:30 AM by KoopLaFez. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 03-10-2008 09:18 AM

    • KoopLaFez
    • Not Ranked
      Male
    • Joined on 01-22-2008
    • San Diego, CA
    • Posts 21

    Referring to parent object in JSCript

    Is there a way in JScript to refer to the current object's parent within a method of that object without explicitly passing the parent object to that method?  For instance: if I am in a method of an object, I can refer to the current object using the this keyword.  What I would like to do is use something along the lines of this.parent to refer to the current object's parent without having to pass it to the method.

    Here's an example:


    //oTest.oChild

    function displayVal(oTest){

               XDocument.UI.Alert(oTest.val1);

    }

    function oChild(){
    //oChild object definition

              this.displayVal = displayVal;
     
    }


    //oTest

    function oTest(){
    //oTest object definition

              this.val1 = "one"; 
              this.child = new oChild();

    }

    oChild is a child object of oTest.  The displayVal method of the oChild object refers to the val1 property of oTest.  In order to do this now, I must explicitly pass the oTest object to the displayVal method when I call it.

     var myTest = new oTest();
     myTest.child.displayVal(myTest);

     What I would like to do is change the displayVal method so that it refers to the oTest object from within the oChild object, so it would look something like this (in pseudocode):

    function displayVal(){

               XDocument.UI.Alert(this.parent.val1);
     
    }

    Then, when I called the method, I wouldn't have to pass the parent object to it:

    var myTest = new oTest();
    myTest.child.displayVal();

     

    Is such a thing possible?  Is it even desireable?  Or is my need to refer to a property on a parent object symptomatic of a larger deficiency in my object structure?

     

  • 03-10-2008 09:30 AM In reply to

    • KoopLaFez
    • Not Ranked
      Male
    • Joined on 01-22-2008
    • San Diego, CA
    • Posts 21

    Re: Referring to parent object in JSCript

    Thinking on this some more, I suppose I could do this:

    //oTest.oChild

    function displayVal(){

               XDocument.UI.Alert(this.val1);

    }

    function oChild(oTest){
    //oChild object definition

              this.displayVal = displayVal;
              this.val1 = oTest.val1;
     
    }


    //oTest

    function oTest(){
    //oTest object definition

              this.val1 = "one";
              this.child = new oChild(this);

    }

    This would save me from having to pass the parent object when I am calling the displayVal method in code; however, I still have to pass the oTest object to the oChild object within the oTest object definition.  Plus, I have to define val1 twice: once in the parent object and once in the child object.

Page 1 of 1 (2 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.