Connecting Tech Pros Worldwide Help | Site Map

Javascript Object - Syntax Help

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 11th, 2008, 08:55 PM
SirCodesALot
Guest
 
Posts: n/a
Default Javascript Object - Syntax Help

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

  #2  
Old March 11th, 2008, 09:15 PM
Joost Diepenmaat
Guest
 
Posts: n/a
Default 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/
  #3  
Old March 11th, 2008, 09:15 PM
Joost Diepenmaat
Guest
 
Posts: n/a
Default 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/
  #4  
Old March 11th, 2008, 11:45 PM
SirCodesALot
Guest
 
Posts: n/a
Default 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!!
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.