Mailing List Archive
tlug.jp Mailing List tlug archive tlug Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]Re: [tlug] Variable scope in Javascript driving me nuts
- Date: Fri, 27 Aug 2010 16:05:23 +0100
- From: Josh Glover <jmglov@example.com>
- Subject: Re: [tlug] Variable scope in Javascript driving me nuts
- References: <4C769657.5040705@example.com> <AANLkTi=MpJfrodyvEZw7urO7yED-vOKfYMaVUJqwMJ2t@example.com>
On 26 August 2010 18:28, Romeo Theriault <romeo.theriault@example.com> wrote: > On Fri, Aug 27, 2010 at 01:29, Dave M G <dave@example.com> wrote: >> >> my function() >> { >> var valid = false; >> string = JSON.stringify(data); >> $.post('jsonhandler.php', {JSON: string}, checkResult, "json"); >> function checkResult(jsonDataReceived) >> { >> valid = jsonDataReceived.valid; >> console.log ("jsonDataReceived.valid = " + jsonDataReceived.valid); >> console.log ("valid inside checkResult = " + valid); >> } > > Just a guess but it looks like you're setting 'var valid = false;' inside a > function as a local variable. That's what 'var' does in javascript I think. > Then inside your inner checkResult function it looks like you're setting > 'valid' as a global function by not having any 'var' in front of it. So the > checkResult's 'valid' variable is a global var and the 'my function()', > 'valid' var is a local variable. This is a related question for a language nerd: does Javascript have closures at all? If anonymous functions are not the way to create a closure, what is? -- Cheers, Josh
- Follow-Ups:
- Re: [tlug] Variable scope in Javascript driving me nuts
- From: Stephen J. Turnbull
- References:
- [tlug] Variable scope in Javascript driving me nuts
- From: Dave M G
- Re: [tlug] Variable scope in Javascript driving me nuts
- From: Romeo Theriault
Home | Main Index | Thread Index
- Prev by Date: Re: [tlug] Die, "Adobe Bridge Workspace File"! Die!
- Next by Date: Re: [tlug] Die, "Adobe Bridge Workspace File"! Die! [FLAWLESS VICTORY]
- Previous by thread: Re: [tlug] Variable scope in Javascript driving me nuts
- Next by thread: Re: [tlug] Variable scope in Javascript driving me nuts
- Index(es):
Home Page Mailing List Linux and Japan TLUG Members Links