Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Javascript Object - Syntax Help

Reply
Thread Tools

Javascript Object - Syntax Help

 
 
SirCodesALot
Guest
Posts: n/a
 
      03-11-2008
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
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      03-11-2008
SirCodesALot <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      03-11-2008
Joost Diepenmaat <(E-Mail Removed)> 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/
 
Reply With Quote
 
SirCodesALot
Guest
Posts: n/a
 
      03-11-2008
On Mar 11, 4:10*pm, Joost Diepenmaat <(E-Mail Removed)> wrote:
> Joost Diepenmaat <(E-Mail Removed)> 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
 
morbidKK
Guest
Posts: n/a
 
      03-12-2008
On Mar 12, 2:06*am, Joost Diepenmaat <(E-Mail Removed)> wrote:
> SirCodesALot <(E-Mail Removed)> 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
 
Henry
Guest
Posts: n/a
 
      03-12-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax Checker that's better than the normal syntax checker Jacob Grover Ruby 5 07-18-2008 05:07 AM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments