From: Johannes Baagoe on
Johannes Baagoe :

> I don't understand the `fact` in the first line.

Got it. Perhaps not the clearest way to declare a local variable, though.

--
Johannes
From: Garrett Smith on
Richard Cornford wrote:
> Johannes Baagoe wrote:
>> Should it be
>>
>> (function foo() { /*...*/ })();
>>
>> or
>>
>> (function foo() { /*...*/ }());
>>
>> (note the place of the closing parenthesis) ?
>>
>> Both are AFAICT syntactically correct and indeed equivalent, but
>> I would tend to prefer the latter: since we are warning the future
>> reader "Beware! This function is not only defined / declared but
>> also called right away!", why not make it quite clear where the
>> scope of that warning ends?
>
> As I am responsible for that construct's use in browser scripting I

The only way for that to be true would be for nobody else to be capable
of realizing the possibility of such constructs.

I used anonymous-and-immediately-invoked-function and long before I had
ever heard of anyone else doing that.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Johannes Baagoe on
Garrett Smith :
> Richard Cornford :

>> As I am responsible for that construct's use in browser scripting

> I used anonymous-and-immediately-invoked-function and long before I had
> ever heard of anyone else doing that.

It seems extremely likely that several persons have invented that
construct independently, like in many other cases.

The earliest (2002-03-01 12:24:33 +0100) *published* occurrence that
I know (Richard Cornford provided it last time the subject arose) is
"Gosha"'s (my indentation)

document.images[0].onclick=(
function (o){return function(){o.handler()}}
)(this)

http://groups.google.fr/group/comp.lang.javascript/msg/53a87649e279a012

--
Johannes
From: Johannes Baagoe on
Johannes Baagoe :

> (function () {
> var fact = function(n) {
> return n > 0 ? n * fact(n - 1) : 1;
> }
> return fact;
> }()(21))

Actually, if we weren't playing games, there would be two cases.

1. 21! is the only factorial we need.

In that case, we may want to prevent the factorial function from
becoming global, since it only serves once.

This might serve - it is IMHO clearer than all other proposed
solutions, and quite as efficient :

var fact21 = (function() {
function fact(n) {
return n > 0 ? n * fact(n - 1) : 1;
};
return fact(21);
}());

However, nothing beats

var fact21 = 51090942171709440000; // factorial(21), a.k.a. 21!

2. 21! is not the only factorial we need.

In that case, the pedestrian

function fact(n) {
return n > 0 ? n * fact(n - 1) : 1;
}

var fact21 = fact(21);

is likely to be the easiest to understand and maintain.

It assumes however that we can be sure that `fact` is always being
called with a small enough positive integer. Above 21, it won't return
an exact result due to floating point rounding, more than 170 will
return Infinity, and anything but a positive integer won't make sense
unless we take the trouble to implement the Gamma function. Therefore,
an actual, production factorial function may be much more complicated.

--
Johannes
From: David Mark on
Johannes Baagoe wrote:
> Garrett Smith :
>> Richard Cornford :
>
>>> As I am responsible for that construct's use in browser scripting
>
>> I used anonymous-and-immediately-invoked-function and long before I had
>> ever heard of anyone else doing that.
>
> It seems extremely likely that several persons have invented that
> construct independently, like in many other cases.
>

Very likely. But typically (and certainly in this case) one person can
claim credit for popularizing an approach. Those who did it in a vacuum
notwithstanding.