Object Reference Within Itself

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vunet

    Object Reference Within Itself

    I am still not clear about how to reference an object within another
    object to pass first object to a function:

    var Parent = {
    myFunc : function(){
    alert("Parent = "+this)
    },
    Child : {
    //how to get reference to Parent?
    myChildFunc : function(){
    alert("Parent IS NOT "+this);
    this.myFunc(); //error -this.myFunc is not a function
    }
    },

    }
    window.onload = Parent.Child.my ChildFunc;
  • Lasse Reichstein Nielsen

    #2
    Re: Object Reference Within Itself

    vunet <vunet.us@gmail .comwrites:
    I am still not clear about how to reference an object within another
    object to pass first object to a function:
    >
    var Parent = {
    myFunc : function(){
    alert("Parent = "+this)
    },
    Child : {
    //how to get reference to Parent?
    myChildFunc : function(){
    alert("Parent IS NOT "+this);
    this.myFunc(); //error -this.myFunc is not a function
    }
    },
    >
    }
    window.onload = Parent.Child.my ChildFunc;
    The way it's written, the function won't even know the "Child" object
    when it's called.

    The same function can be a property of many objects at the same time
    (as can any other object). There is no way to go from the function
    code to anything but the object it was called as a method of (i.e.,
    "this"). The references go in the other direction.

    /L
    --
    Lasse Reichstein Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • vunet

      #3
      Re: Object Reference Within Itself

      On Sep 16, 2:02 pm, Lasse Reichstein Nielsen <lrn.unr...@gma il.com>
      wrote:
      vunet <vunet...@gmail .comwrites:
      I am still not clear about how to reference an object within another
      object to pass first object to a function:
      >
      var Parent = {
        myFunc : function(){
          alert("Parent = "+this)
        },
        Child : {
          //how to get reference to Parent?
          myChildFunc : function(){
             alert("Parent IS NOT "+this);
             this.myFunc(); //error -this.myFunc is not a function
          }
        },
      >
      }
      window.onload = Parent.Child.my ChildFunc;
      >
      The way it's written, the function won't even know the "Child" object
      when it's called.
      >
      The same function can be a property of many objects at the same time
      (as can any other object). There is no way to go from the function
      code to anything but the object it was called as a method of (i.e.,
      "this"). The references go in the other direction.
      >
      /L
      --
      Lasse Reichstein Nielsen
       DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'
      So how would I write the function within an object B which is within
      an object A, which (i.e. the function) would would have a reference to
      object A methods?

      Comment

      • vunet

        #4
        Re: Object Reference Within Itself

        On Sep 16, 3:15 pm, vunet <vunet...@gmail .comwrote:
        On Sep 16, 2:02 pm, Lasse Reichstein Nielsen <lrn.unr...@gma il.com>
        wrote:
        >
        >
        >
        vunet <vunet...@gmail .comwrites:
        I am still not clear about how to reference an object within another
        object to pass first object to a function:
        >
        var Parent = {
          myFunc : function(){
            alert("Parent = "+this)
          },
          Child : {
            //how to get reference to Parent?
            myChildFunc : function(){
               alert("Parent IS NOT "+this);
               this.myFunc(); //error -this.myFunc is not a function
            }
          },
        >
        }
        window.onload = Parent.Child.my ChildFunc;
        >
        The way it's written, the function won't even know the "Child" object
        when it's called.
        >
        The same function can be a property of many objects at the same time
        (as can any other object). There is no way to go from the function
        code to anything but the object it was called as a method of (i.e.,
        "this"). The references go in the other direction.
        >
        /L
        --
        Lasse Reichstein Nielsen
         DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'
        >
        So how would I write the function within an object B which is within
        an object A, which (i.e. the function) would would have a reference to
        object A methods?
        Details of what I need:

        var A = {
        B : {
        f : function(){
        this.Fn(); // <== ERROR REFERENCING, HELP!
        //I understand "this" in this case is a reference to object "B"
        not "A"
        //How to get reference to A?
        }
        },
        Fn : function(){
        alert("Hey!");
        }
        }

        Comment

        • Joost Diepenmaat

          #5
          Re: Object Reference Within Itself

          vunet <vunet.us@gmail .comwrites:
          Details of what I need:
          >
          var A = {
          B : {
          f : function(){
          this.Fn(); // <== ERROR REFERENCING, HELP!
          //I understand "this" in this case is a reference to object "B"
          not "A"
          correct, at least, if you call it as A.B.f() - there are plenty of ways
          to call that function where this may refer to something else completely,
          including A.
          //How to get reference to A?
          see below.
          }
          },
          Fn : function(){
          alert("Hey!");
          }
          }
          You can't get a reference to A from B, unless you create a reference
          yourself. You can think of properties as analogous to singly linked
          lists. You can get from A to B, but there is no reference back.

          You may want something like:

          var A = {
          B : {
          f : function(){
          this.parent.Fn( );
          }
          },
          Fn : function(){
          alert("Hey!");
          }
          }

          A.B.parent = A;
          A.B.f();


          --
          Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

          Comment

          • showellshowell@gmail.com

            #6
            Re: Object Reference Within Itself

            On Sep 16, 8:29 am, vunet <vunet...@gmail .comwrote:
            I am still not clear about how to reference an object within another
            object to pass first object to a function:
            >
            var Parent = {
              myFunc : function(){
                alert("Parent = "+this)
              },
              Child : {
                //how to get reference to Parent?
                myChildFunc : function(){
                   alert("Parent IS NOT "+this);
                   this.myFunc(); //error -this.myFunc is not a function
                }
              },
            >
            }
            >
            window.onload = Parent.Child.my ChildFunc;
            Something like this might work for you.

            function Parent() {
            this.fruit = 'apple'

            parentObj = this;
            this.myFunc = function(){
            alert("Fruit = "+this.frui t);
            };
            this.Child = {
            myChildFunc : function(){
            parentObj.myFun c();
            }
            };
            }

            window.onload = new Parent().Child. myChildFunc;

            Steve


            Comment

            • vunet

              #7
              Re: Object Reference Within Itself

              On Sep 16, 10:25 pm, "showellshow... @gmail.com"
              <showellshow... @gmail.comwrote :
              On Sep 16, 8:29 am, vunet <vunet...@gmail .comwrote:
              >
              >
              >
              I am still not clear about how to reference an object within another
              object to pass first object to a function:
              >
              var Parent = {
                myFunc : function(){
                  alert("Parent = "+this)
                },
                Child : {
                  //how to get reference to Parent?
                  myChildFunc : function(){
                     alert("Parent IS NOT "+this);
                     this.myFunc(); //error -this.myFunc is not a function
                  }
                },
              >
              }
              >
              window.onload = Parent.Child.my ChildFunc;
              >
              Something like this might work for you.
              >
              function Parent() {
                this.fruit = 'apple'
              >
                parentObj = this;
                this.myFunc = function(){
                  alert("Fruit = "+this.frui t);
                };
                this.Child = {
                  myChildFunc : function(){
                    parentObj.myFun c();
                  }
                };
              >
              }
              >
              window.onload = new Parent().Child. myChildFunc;
              >
              Stevehttp://webstervanrobot .com/
              Interesting. So when initializing a function as parent of objects it
              kinda works but having a global object as parent does not... My idea
              was to separate objects' logic within one global parent object but it
              looks like I need to apply those references or convert them into
              function objects.
              Thanks

              Comment

              • slebetman

                #8
                Re: Object Reference Within Itself

                On Sep 17, 3:28 am, vunet <vunet...@gmail .comwrote:
                On Sep 16, 3:15 pm, vunet <vunet...@gmail .comwrote:
                So how would I write the function within an object B which is within
                an object A, which (i.e. the function) would would have a reference to
                object A methods?
                >
                Details of what I need:
                >
                var A = {
                B : {
                f : function(){
                this.Fn(); // <== ERROR REFERENCING, HELP!
                //I understand "this" in this case is a reference to object "B"
                not "A"
                //How to get reference to A?
                }
                },
                Fn : function(){
                alert("Hey!");
                }
                >
                }
                Just reference A directly:

                var A = {
                B : {
                f : function () {
                A.Fn(); // <-----
                }
                },
                Fn : function () {
                alert("Hey!");
                }
                }

                Btw, using 'this' in javascript may or may not do what you want:

                A.B.f() // 'this' refers to A.B
                document.body.o nclick = A.B.f // 'this' refers to document.body

                Comment

                Working...