[Javascript] That pesky closure thing :(

Andrew Ip andrewsyip at gmail.com
Mon Sep 28 14:47:00 CDT 2009


You should be able to just call:

processing(forms[i])

since the anonymous function you're assigning to your Ajax onLoading
variable is able to see the variables that's bound within the body of
the ajaxForms function.  And yes, this is because a closure is formed.
 :)

-andrew


On Mon, Sep 28, 2009 at 11:23 AM, Terry Riegel
<riegel at clearimageonline.com> wrote:
> Hello Everyone,
>
> I have a new project I am working on and I think the word closure will
> come up in the solution. Something I have not been able to get my head
> around.
>
> I have this function
>
>  function ajaxForms(c) {
>  var forms= document.getElementsByTagName('form');
>  for (i=0; i<forms.length; i++)
>  {
>   if (forms[i].className.indexOf(c) != -1)
>   {
>    forms[i].onsubmit=function ()
>    {
>     new Ajax.Request(
>       this.action,
>       {
>        method: this.method,
>        parameters: 'ajaxrequest=TRUE&'+riegelSerialize(this),
>        encoding: 'UTF-8',
>        onLoading: function (){if(typeof processing == "function")
> {processing(this)}   }
>       }
>        );
>     return false;
>    }
>   }
>  }
> }
>
> I want to take that onLoading line and have it run a function called
> processing. The problem I am having is I want to pass "this" to the
> processing function. I want this to be the form object forms[i].
>
> Any ideas on how I can accomplish this.
>
> Thanks,
>
> Terry Riegel
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript
>



More information about the Javascript mailing list