[Dojo-interest] javascript : copy or reference

Rhapidophyllum rhapidophyllum at cox.net
Thu Feb 8 12:39:37 MST 2007


Thanks, makes sense.
So was I incorrect in saying the part in parentheses?

a function has access to ... 3) local variables of an enclosing  
function (but not local variables of a function that encloses that  
enclosing function...)



On Feb 8, 2007, at 2:16 PM, Jonathan Bond-Caron wrote:

The problem is your variable ‘i’ is passed by reference.

With
dojo.event.connect(span2, 'onclick', function(e) {getClientList(i);});
or
dojo.event.connect(span2, 'onclick', getClientList(i) );

Your saying “On the onclick event of the element span2, execute  
getClientList and pass the argument i”

However before you click the span element, the variable i has changed  
(finished it’s loop)  :
for(var i=1; i<=8; i++)

So when getClientList(i) gets called, it looks up the ‘pointer’ or  
reference to i and gets its value -- which is now 9

Hope that clears it up,
j

From: dojo-interest-bounces at dojotoolkit.org [mailto:dojo-interest- 
bounces at dojotoolkit.org] On Behalf Of Rhapidophyllum
Sent: February 8, 2007 1:39 PM
To: dojo-interest at dojotoolkit.org
Subject: Re: [Dojo-interest] javascript : copy or reference

Thanks for the tip, I went and read that (section 8.8.4 in 5th ed.)

It says "All Javascript functions are closures".  Which means that  
all Javascript functions are a combination of code, and the scope in  
which to execute them.
If I understand correctly, the scope a function has access to  
includes 1) global variables, 2) local variables and 3) local  
variables of an enclosing function (but not local variables of a  
function that encloses that enclosing function. It doesn't explicitly  
say this--is this correct?)
If this is correct, in the original example, the variable i cannot be  
accessed (or at least accessed as it is being incremented) because it  
is not in the enclosing function:

                                     for(var i=1; i<=8; i++) {
                                                 var span2 =  
document.createElement("span");
                                                 dojo.byId 
('pagelist').appendChild(span2);
                                                 dojo.event.connect 
(span2, 'onclick', function(e) {getClientList(i);});
                                     }

So to ask a possibly naive question, why create an enclosing  
function?  Should the following work?

                                                 dojo.event.connect 
(span2, 'onclick', getClientList(i) );


On Feb 8, 2007, at 12:13 PM, Karl Tiedt wrote:

For an understanding of Closures and you really should read  
"Javascript: The definitive guide" I believe Chapter 2 in it covers  
Closures and how to use/work with them, *ANYNONE* developing  
something that uses JS should have knowledge of this otherwise you  
will spend more time hunting down the problems not using them causes  
than designing your application itself.

-Karl

On 2/8/07, Thibaut <thibaut at fuse.co.uk> wrote:
Thank you very much !!
it works now

On Feb 8, 2007, at 1:43 PM, Nicola Rizzo wrote:

 > Sorry,
 > dojo.event.connect(span2,'onclick',dojo.lang.hitch(span2,function(e)
 > {getClientList( this.cnt)}));
 >
 >
 > On 2/8/07, Thibaut <thibaut at fuse.co.uk> wrote:
 >> I'm not in an object. I juste made javascript functions in my html
 >> page.
 >> I tried your solution but this.cnt is undefined
 >>
 >> On Feb 8, 2007, at 12:03 PM, Nicola Rizzo wrote:
 >>
 >> > It's a closure.
 >> > Try this:
 >> > for(...){
 >> >    span2.cnt = i;
 >> >    dojo.event.connect(span2,'onclick',function(e){getClientList
 >> > (this.cnt)});
 >> > ...
 >> > }
 >> >
 >> > hth,
 >> >      Nicola
 >> >
 >> > On 2/8/07, Thibaut <thibaut at fuse.co.uk> wrote:
 >> >> Hi
 >> >>
 >> >> In the following example the "i" variable seems to be passed by
 >> >> reference to the getClientList(i)
 >> >> Therefore getClientList(9) is always called !
 >> >>
 >> >>                         for(var i=1; i<=8; i++) {
 >> >>                                 var span2 =  
document.createElement
 >> >> ("span");
 >> >>                                 dojo.byId('pagelist').appendChild
 >> >> (span2);
 >> >>                                 dojo.event.connect(span2,
 >> >> 'onclick', function(e) {getClientList
 >> >> (i);});
 >> >>                         }
 >> >>
 >> >>
 >> >> How can i manage to call getClientList(1), getClientList(2) ...
 >> >> getClientList(8) ?
 >> >>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://dojotoolkit.org/pipermail/dojo-interest/attachments/20070208/f6604e53/attachment-0001.html


More information about the Dojo-interest mailing list