From: Johannes Baagoe on
Stefan Weiss :
>>> Johannes Baagoe :

>>>> function expressions with a name are a Really Bad Idea

> The returned inner function in your examples doesn't need a name, so you
> don't give it one.

It seems that we agree, after all.

--
Johannes
From: Johannes Baagoe on
Thomas 'PointedEars' Lahn :
> Johannes Baagoe :

>> var foo = (function() { /*...*/ })();
>>
>> vs.
>>
>> var foo = (function() { /*...*/ }());

> Currently I am using such assignments more often than one of the
> statements above, although to real properties (which become methods),
> seldom to variables.

<rant>

If I were to teach elementary programming these days (perhaps
fortunately for the students, I don't any longer), I would 1. use
javascript - or rather, ECMASCript in whatever version seems the most
reasonable, probably still 3rd - as "first" language, and 2. only
introduce function *declarations* at the very end of the course, as a
kind of afterthought - "You can also do it that way, and it may even
help older programmers who don't know better", or something like that.

This exposes the student from the very start to the idea that functions
are very, very important animals, quite as important as numbers and
strings, and that they can be assigned, stored in objets (in which
case we call them "methods" rather than "properties", but that hasn't
necessarily to be a big deal), passed as arguments to other functions
or even to themselves, etc.

Which is a most important notion if the student is ever to do
functional programming, lambda calculus, about any kind of maths, and
even philosophy or linguistics, where a functional view of language -
words and sentences as procedures, that is, parametrised behaviour
triggered by events - is a useful contrast to the more traditional
view of language as a representation of the world.

</rant>

--
Johannes
From: Johannes Baagoe on
nick :
> Johannes Baagoe :

>>   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:
>
> 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"

Quite, but even assuming correct implementations, I would still add the
parentheses (second version), as a help to myself, to programmers who
are new to javascript, and even to old javascript hands who recognise
the idiom. When one sees "(function", it means "this function is
going to be called right away - its body ends at the closing brace
that matches the first opening brace, and the entire construct ends
at the closing parenthesis that matches this one".

It is especially important if the result is not a Function, like

var two = (function() {return 2;}());

Taking all that trouble be pointless in this case, but since
javascript's scope of variables are functions, not blocks, and since
the usual way to provide other languages' notion of static variables
is closures, it is very often a useful idiom - one that must be
taught to anybody who starts on javascript, especially when coming
from other languages with a C-style syntax.

--
Johannes
From: Johannes Baagoe on
Stefan Weiss :

> Names for function expressions are useful in at least
> two cases: for debugging/profiling,

That could indeed be the case with some debuggers/profilers, I would
consider them less than satisfactory. But see below.

> and when you want to recurse from inside a function expression.

What is wrong with

var fact = function(n) {
if (n > 0) {
return n * fact(n - 1);
} else {
return 1;
}
};

?

> The only alternative for the second case would be arguments.callee,
> which (I think) has recently been deprecated.

I think I just provided another and possibly better alternative.

--
Johannes
From: Johannes Baagoe on
Jorge :
> nick :

>> "Some JavaScript engines, not including SpiderMonkey, incorrectly treat
>> any function expression with a name as a function definition"

> 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.
>
> 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?),
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.

--
Johannes