HighDots Forums  

Javascript Object - Syntax Help

Javascript JavaScript language (comp.lang.javascript)


Discuss Javascript Object - Syntax Help in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
SirCodesALot
 
Posts: n/a

Default Javascript Object - Syntax Help - 03-11-2008 , 04:54 PM






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

Reply With Quote
  #2  
Old   
Joost Diepenmaat
 
Posts: n/a

Default Re: Javascript Object - Syntax Help - 03-11-2008 , 05:06 PM






SirCodesALot <sjourdan (AT) gmail (DOT) com> writes:

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/


Reply With Quote
  #3  
Old   
Joost Diepenmaat
 
Posts: n/a

Default Re: Javascript Object - Syntax Help - 03-11-2008 , 05:10 PM



Joost Diepenmaat <joost (AT) zeekat (DOT) nl> writes:

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/


Reply With Quote
  #4  
Old   
SirCodesALot
 
Posts: n/a

Default Re: Javascript Object - Syntax Help - 03-11-2008 , 07:36 PM



On Mar 11, 4:10*pm, Joost Diepenmaat <jo... (AT) zeekat (DOT) nl> wrote:
Quote:
Joost Diepenmaat <jo... (AT) zeekat (DOT) nl> 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!!


Reply With Quote
  #5  
Old   
morbidKK
 
Posts: n/a

Default Re: Javascript Object - Syntax Help - 03-12-2008 , 07:16 AM



On Mar 12, 2:06*am, Joost Diepenmaat <jo... (AT) zeekat (DOT) nl> wrote:
Quote:
SirCodesALot <sjour... (AT) gmail (DOT) com> 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"


Reply With Quote
  #6  
Old   
Henry
 
Posts: n/a

Default Re: Javascript Object - Syntax Help - 03-12-2008 , 09:09 AM



On Mar 12, 11:16 am, morbidKK wrote:
Quote:
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).

Quote:
.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.


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.