Hi again ColinFine, Just had another test that make me understand what you quote:
var _f=function(){return true}; var F; if(_f()===true) F=function(){alert('F true');} else F=function(){alert('F false');}; F(); var _ff=true; if((function(){return false})()===true) { function FF(){alert('FF true');} } else { function FF(){alert('FF false');}; } FF(); function F(){alert('new F');} F(); F=function(){alert('last F');} F(); first alert return true, second false. For the next alert, received: 'F true' and the last one 'last F'. But it seems not abnormal, while assigning a function to a variable is done at execution time, and defining a statement functionis done at parse time. I think this is what was saying crokford in your quote. But that seems not abnormal, and even not EVIL ... but that's my poor opinion. -- david On 27 mai, 12:48, david <david.brill...@gmail.com> wrote: > Hi ColinFine, > > > It doesn't seem to me that these imply that there will be a different > > scope, but I'm not sure. > > I translate badlly my though (and I'm probably wrong). The created > function in both case have the same scope (I think too). > > According to what you quote, I made the following test: > var _f=function(){return true}; > > var F; > if(_f()===true) F=function(){alert('F true');} > else F=function(){alert('F false');}; > F(); > > var _ff=true; > > if((function(){return false})()===true) { function FF(){alert('FF > true');} } > else { function FF(){alert('FF false');}; } > FF(); > > and in both case it work as expected : first 'true' and second > 'false'. > Means you could use function in the if test, inside the if statement > and both way of writting the function have the same result. > > > - "function statmenes are subject to 'hoisting'. This means that > > regardless of where a function is placed, it is moved to the top of > > the scope in which it is defined. ... It also prohibits the use of > > function statements in if statements. ... " > > This part doesn't seems to me a bad things, it permit to define a > function after it's usage (in the created code, not during execution > of code of course). > > I've perhaps miss something ??? > > -- > david > > On 26 mai, 14:06, ColinFine <colin.f...@pace.com> wrote: > > > On May 26, 12:41 pm, david <david.brill...@gmail.com> wrote: > > > > @ColinFine, > > > > > According to Flanagan's book (section 8.1.2) the optional function- > > > > name in a function literal is not assigned to a variable, but > > > > apparently lives in a special namespace of its own that allows the > > > > function to refer to itself. So unless the function is recursive, the > > > > above is identical to > > > > > var myFunction = function() {alert('hi');} > > > > > and more or less identical to > > > > > function myFunction() {alert('hi')} > > > > I try to refer to myFunction recursivelly and in both case it works > > > (test done on FF3.0). > > > could you send me an exemple of what you say ? > > > No I can't. I'm just quoting from the books. > > > > I'd only though the difference was in the scope of the created > > > function depending on the way it is declare as a *statement* or as a > > > *litteral*. > > > I didn't go into the reasons why Crockford advises not using function > > statements. They are: > > - "the second form makes it clear that [myFunction] is a variable > > containing a function value. To use the language well, it is important > > to understand that functions are values" > > - "function statmenes are subject to 'hoisting'. This means that > > regardless of where a function is placed, it is moved to the top of > > the scope in which it is defined. ... It also prohibits the use of > > function statements in if statements. ... " > > > It doesn't seem to me that these imply that there will be a different > > scope, but I'm not sure. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---