Assuming you mean something like this?

$('someElement').click(function()
{
   //now in anonymous function
   var x = 0;

   //do a bunch of stuff

   x = 2;
}
);

If so, yes, this is valid.

On Apr 19, 11:59 am, Ariel Jakobovits <[EMAIL PROTECTED]> wrote:
> so is it ok to rely on this behavior in javascript?
>
> ----- Original Message ----
> From: Rob Desbois <[EMAIL PROTECTED]>
> To: jquery-en@googlegroups.com
> Sent: Wednesday, April 18, 2007 1:52:21 AM
> Subject: [jQuery] Re: vars in unnamed functions
>
> 17By creating a function form your code which uses variables outside its 
> scope, you've created a 'closure'.
>
> When settings.functionToCallLater() is called after init() has finished, 
> aVariable has gone out of scope. The function still has access to it however, 
> because the closure consists of two parts: the code of the function itself, 
> and a reference to all variables outside its scope that it needs access to.
>
> Have a search on Google for 'javascript closures', there are plenty of good 
> articles that can explain this more fully than me.
>
> HTH,
> rob
>
> On 4/18/07,
> Ariel Jakobovits <[EMAIL PROTECTED]> wrote:
>
> this isn't the real code, just a mockup, but ...
>
> can someone explain to me why this works (as in, alert echoes 'ariel'):
>
> var temp = null;
>
> function init ()
> {
>
>     var aVariable = 'ariel';
>
>     var settings = {
>             functionToCallLater: function (content) {
>                 alert(aVariable);
>             }
>         };
>
>     temp = new anotherClass(settings);
> }
>
> anotherClass.callThatFunction
> ();
>
> --
> Rob Desbois
> Eml: [EMAIL PROTECTED]
> Tel: 01452 760631
> Mob: 07946 705987
> "There's a whale there's a whale there's a whale fish" he cried, and the 
> whale was in full view.
>
> ...Then ooh welcome. Ahhh. Ooh mug welcome.

Reply via email to