Javascript Object - Syntax Help

S

SirCodesALot

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
 
J

Joost Diepenmaat

SirCodesALot said:
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
}
}
}
}
 
J

Joost Diepenmaat

Joost Diepenmaat said:
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
 
S

SirCodesALot

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


Anyway.
HTH

Thanks a alot for your reponse. Just what I needed!!
 
M

morbidKK

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
               }
        }
  }

}

but thats not necessarily a clousre..function being a first class
object in Javascript you can access it using "this"
 
H

Henry

On Mar 12, 2:06 am, Joost Diepenmaat wrote:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top