Connecting Tech Pros Worldwide Help | Site Map

Javascript Object - Syntax Help

SirCodesALot
Guest
 
Posts: n/a
#1: Mar 11 '08
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
Joost Diepenmaat
Guest
 
Posts: n/a
#2: Mar 11 '08

re: Javascript Object - Syntax Help


SirCodesALot <sjourdan@gmail.comwrites:
Quote:
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
Guest
 
Posts: n/a
#3: Mar 11 '08

re: Javascript Object - Syntax Help


Joost Diepenmaat <joost@zeekat.nlwrites:
Quote:
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
Quote:
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/
SirCodesALot
Guest
 
Posts: n/a
#4: Mar 12 '08

re: Javascript Object - Syntax Help


On Mar 11, 4:10*pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
Quote:
Joost Diepenmaat <jo...@zeekat.nlwrites:
Quote:
just use a closure:
>
Quote:
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
>
Quote:
* * * * 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!!
Closed Thread