From: Stefan Weiss on 20 Apr 2010 14:04 On 20/04/10 19:31, Johannes Baagoe wrote: > In a sort of lame defence (why on earth am I trying to excuse Microsoft?), > it may be argued that function expressions with a name are a Really Bad > Idea from the beginning, and that anyone who uses them deserves... well, > some terrible fate. Why? The only disadvantage I see are the problems with Microsoft's botched implementation. Didn't you say (in a different thread) that you "quite liked" Jorge's loop() function? That's a named function expression, too. It doesn't have to be - you could just call loop() separately from its definition - but the way you're using it on http://baagoe.com/en/RandomMusings/javascript/time.js it's still a named function expression. Do you deserve a terrible fate now? ;-) -- stefan
From: Johannes Baagoe on 20 Apr 2010 14:08 Jorge : > Johannes Baagoe : >> In a sort of lame defence (why on earth am I trying to excuse >> it may be argued that function expressions with a name are >> a Really Bad Idea from the beginning, and that anyone who uses them >> deserves... well, some terrible fate. > Huh ? > How so ? var fibonacci = (function() { var a = 1; var b = 1; return function() { a = b - a; return b += a; }; }()); is fine. var fibonacci = (function fib() { var a = 1; var b = 1; return function() { a = b - a; return b += a; }; }()); or var fibonacci = (function() { var a = 1; var b = 1; return function fib() { a = b - a; return b += a; }; }()); or (even worse) var fibonacci = (function fib() { var a = 1; var b = 1; return function fib() { a = b - a; return b += a; }; }()); are useless and confusing - what is fib ? -- Johannes
From: Garrett Smith on 20 Apr 2010 14:37 Johannes Baagoe wrote: > Jorge : >> nick : > >>> "Some JavaScript engines, not including SpiderMonkey, incorrectly treat >>> any function expression with a name as a function definition" > That behavior, as descriped, is not incorrect. >> LOL. And these engines are made by Microsoft and are called JScript and >> are the ones used in each and every Internet Explorer, up to and >> including the current, latest one. >> The behavior described is not incorrect. >> And they manage miraculously to not only screw up the function >> expression, but the function declaration too. Wonders of software >> engineering. > > In a sort of lame defence (why on earth am I trying to excuse Microsoft?), If you're asking me to guess, I would say that you have probably mistaken "function definition" with "function declaration". I would also speculate that the author(s) of that (badly written) MDC page may have had the same mistake (or similar lazy, muddled thinking). Microsoft may have related bugs, but they are described neither in this thread nor on MDC. The MDC page and the responses on this thread (including yours) are an exhibition of misunderstanding of the specification. How ironic, and hypocritical now, when the responses on this thread call out Microsoft for exhibiting a misinterpretation of the specification. I suggest you to and read the ECMA-262 specification for "Function Definition". [...] -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Stefan Weiss on 20 Apr 2010 14:38 On 20/04/10 20:08, Johannes Baagoe wrote: > Jorge : >> Johannes Baagoe : >>> In a sort of lame defence (why on earth am I trying to excuse >>> it may be argued that function expressions with a name are >>> a Really Bad Idea from the beginning, and that anyone who uses them >>> deserves... well, some terrible fate. > >> Huh ? >> How so ? > > var fibonacci = (function() { > var a = 1; var b = 1; > return function() { > a = b - a; > return b += a; > }; > }()); > > is fine. > > var fibonacci = (function fib() { > var a = 1; var b = 1; > return function() { > a = b - a; > return b += a; > }; > }()); > > or > > var fibonacci = (function() { > var a = 1; var b = 1; > return function fib() { > a = b - a; > return b += a; > }; > }()); > > or (even worse) > > var fibonacci = (function fib() { > var a = 1; var b = 1; > return function fib() { > a = b - a; > return b += a; > }; > }()); > > are useless and confusing - what is fib ? Well, don't write useless and confusing code, then :) The returned inner function in your examples doesn't need a name, so you don't give it one. Names for function expressions are useful in at least two cases: for debugging/profiling, and when you want to recurse from inside a function expression. The only alternative for the second case would be arguments.callee, which (I think) has recently been deprecated. Even if it hasn't: making use of the arguments object has a noticeable performance penalty in modern browsers. -- stefan
From: Garrett Smith on 20 Apr 2010 02:20
nick wrote: > On Apr 19, 9:55 pm, Johannes Baagoe <baa...(a)baagoe.com> wrote: > >> Same question for >> >> var foo = (function() { /*...*/ })(); >> >> vs. >> >> var foo = (function() { /*...*/ }()); > > Those shouldn't need the extra parentheses... > > var foo = function() { /*...*/ }(); > > ...should be valid. > > But nobody ever does that, which makes me think some old(ish) browsers > might be more picky, especially after reading this: > The Grouping operator merely provides is a hint at an idiom. Some omit it. > https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Functions > #Function_constructor_vs._function_declaration_vs._function_expression > > "Some JavaScript engines, not including SpiderMonkey, incorrectly > treat any function expression with a name as a function definition" > That in and of itself is not a true statement. Moreover, it is unsupported by the example that follows. | Note: Some JavaScript engines, not including SpiderMonkey, incorrectly | treat any function expression with a name as a function definition. | This would lead to zero being defined, even with the always-false if | condition. A safer way to define functions conditionally is to define | the function anonymously and assign it to a variable: | | if (0) { | var zero = function() { | document.writeln("This is zero."); | } | } The paragraph makes a statement about a function expression with a name being treated as a function definition. That would be a true statement if it had been rewritten as: | Some JavaScript engines, not including SpiderMonkey, incorrectly | parse any function expression with an identifier as a function | declaration. However, even at that, it is still unsupported by the example. The FunctionExpression in the example does not have an identifier. It is an anonymous function expression. The value of `zero` can be expected to be undefined following the block after the `if` statement. For example: if (0) { var zero = function() { document.writeln("This is zero."); } } alert(typeof zero); - can be expected to elert "undefined". So I'm afraid MDC is a bad source of information here. Unfortunately, the Edit feature is non-functional in Firefox with or without javascript enabled. I am now getting: | Service Unavailable | | The service is temporarily unavailable. Please try again later. Hopefully they can fix the errors on MDC. That is pitiful. -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/ |