Javascript Object - Syntax Help

Discussion in 'Javascript' started by SirCodesALot, Mar 11, 2008.

  1. SirCodesALot

    SirCodesALot Guest

    Can someone help me with this syntax, I have the following javascript
    object:

    function foo()
    {
    this.myFooVar = 0;
    this.doThis = function (){
    var ao = new AnotherObject();
    ao.onReadyStateChange = function(){
    if (this.myFooVar == 0) <----------- Here is the
    problem
    {
    // do this
    }
    }
    }
    }

    How do I refer to the "this.myFooVar" from the foo object when
    defining the sub-sub function for on readyStateChange? Did i need to
    use a keyword like parent?

    Thanks in advance for your help.

    -SJ

    I
     
    SirCodesALot, Mar 11, 2008
    #1
    1. Advertising

  2. SirCodesALot <> writes:

    > How do I refer to the "this.myFooVar" from the foo object when
    > defining the sub-sub function for on readyStateChange? Did i need to
    > use a keyword like parent?


    just use a closure:

    function foo()
    {
    this.myFooVar = 0;
    var myFoo = this; // save the this object
    this.doThis = function (){
    var ao = new AnotherObject();
    ao.onReadyStateChange = function(){
    if (myFoo.myFooVar == 0) // access it here
    {
    // do this
    }
    }
    }
    }

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
     
    Joost Diepenmaat, Mar 11, 2008
    #2
    1. Advertising

  3. Joost Diepenmaat <> writes:

    > just use a closure:
    >
    > function foo()
    > {
    > this.myFooVar = 0;
    > var myFoo = this; // save the this object
    > this.doThis = function (){
    > var ao = new AnotherObject();


    var myFoo = this; // or here, which seems like a more
    // logical place

    > ao.onReadyStateChange = function(){
    > if (myFoo.myFooVar == 0) // access it here
    > {
    > // do this
    > }
    > }
    > }
    > }


    Anyway.
    HTH

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
     
    Joost Diepenmaat, Mar 11, 2008
    #3
  4. SirCodesALot

    SirCodesALot Guest

    On Mar 11, 4:10 pm, Joost Diepenmaat <> wrote:
    > Joost Diepenmaat <> writes:
    > > just use a closure:

    >
    > > function foo()
    > > {
    > >   this.myFooVar = 0;
    > >   var myFoo = this; // save the this object
    > >   this.doThis = function (){
    > >         var ao = new AnotherObject();

    >
    >           var myFoo = this;     // or here, which seems like a more
    >                                 // logicalplace
    >
    > >         ao.onReadyStateChange = function(){
    > >                if (myFoo.myFooVar == 0)  // accessit here
    > >                {
    > >                   // do this
    > >                }
    > >         }
    > >   }
    > > }

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


    Thanks a alot for your reponse. Just what I needed!!
     
    SirCodesALot, Mar 11, 2008
    #4
  5. SirCodesALot

    morbidKK Guest

    On Mar 12, 2:06 am, Joost Diepenmaat <> wrote:
    > SirCodesALot <> writes:
    > > How do I refer to the "this.myFooVar" from the foo object when
    > > defining the sub-sub function for on readyStateChange? Did i need to
    > > use a keyword like parent?

    >
    > just use a closure:
    >
    > function foo()
    > {
    >   this.myFooVar = 0;
    >   var myFoo = this; // save the this object
    >   this.doThis = function (){
    >         var ao = new AnotherObject();
    >         ao.onReadyStateChange = function(){
    >                if (myFoo.myFooVar == 0)  // access it here
    >                {
    >                   // do this
    >                }
    >         }
    >   }
    >
    > }
    >
    > --
    > Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/


    but thats not necessarily a clousre..function being a first class
    object in Javascript you can access it using "this"
     
    morbidKK, Mar 12, 2008
    #5
  6. SirCodesALot

    Henry Guest

    On Mar 12, 11:16 am, morbidKK wrote:
    > On Mar 12, 2:06 am, Joost Diepenmaat wrote:

    <snip>
    >> function foo()
    >> {
    >> this.myFooVar = 0;
    >> var myFoo = this; // save the this object
    >> this.doThis = function (){
    >> var ao = new AnotherObject();
    >> ao.onReadyStateChange = function(){
    >> if (myFoo.myFooVar == 0) // access it here
    >> {
    >> // do this
    >> }
    >> }
    >> }

    >
    >> }

    <snip>
    > but thats not necessarily a clousre.


    No, that is a closure (in fact it is two, the first being redundant).

    >.function being a first class
    > object in Javascript you can access it using "this"


    You can access something using - this -, but what is "it" in this
    case? The - this - reference when the - onReadyStateChange - method of
    the - AnotherObject - instance is called will most likely be a
    reference to the - AnotherObject - instance, and so not a reference to
    the - foo - instance that has the - myFooVar - property.
     
    Henry, Mar 12, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gabriele renzi
    Replies:
    2
    Views:
    207
    gabriele renzi
    Dec 31, 2005
  2. Ken Bloom
    Replies:
    3
    Views:
    215
  3. Good Night Moon
    Replies:
    9
    Views:
    284
    Rick DeNatale
    Jul 25, 2007
  4. Jacob Grover
    Replies:
    5
    Views:
    318
    Jacob Grover
    Jul 18, 2008
  5. Mark Richards
    Replies:
    3
    Views:
    316
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page