(function(){
...lines of code...
} ) ();
It is defining an anonymous function then immediately calling it. As
you know, if 'foo' is a function then you can call it with the code
'foo()'. In JavaScript, however, functions are first class (just like
strings or integers) and don't have to have names. So function ()
{...lines of code...} is literally a function just as 2 is literally a
number or 'bar' is literally a string. So you can call function ()
{...lines of code...} just as you would if it were named: by appending
'()' at the end. They could have probably written the code as function
() {...lines of code...}() and gotten the same effect. Also, if the
function takes arguments such as function (x, y, z) {...lines of
code...} then you can call it with code such as (function (x,y,z)
{...})(a,b,c) where a, b, and c are legal JavaScript values.
No, they couldn't. If a statement starts with the identifier
"function" it will be interpreted as a function *declaration* which
must have a name, otherwise a syntax error will result. You can omit
the name in a function *expression*, hence the use of:
(function(){...})();
If Richard Cornford is still lurking you may have coaxed him out of
retirement.
--
Rob
I can understand the anonymous function call
function(){...}
what puzzles me is the surrounding brackets and the following ();
can you explain what these achieve please ?
You are correct. The only time I ever use "function() {...}()" is when
I'm returning a function from another one, hence "function" in that
case is preceded by the word "return". Guess I just never noticed.
So what you are saying is that
(function(){...})();
turns the anonymous function declaration function(){...};
into a function expression
right ?
right.
No. A function declaration can't be anonymous. A function
declaration *must* have a name, and therefore isn't anonymous. A
function expression doesn't need a name - you can provide one but it's
pretty useless (except in IE which, from memory, makes it a global
variable but I may be wrong there).
Maybe examples are better:
<script>
function fnName (){...}
// and
function fnName1(){
function fn2name(){...}
}
</script>
and so on are function declarations, they must have a name, whereas:
<script>
var x = function() {...}
// and
(function(){...})
// and
var x = {}
x.foo = function(){...}
// and
var y = {
bar: function(){...}
}
</script>
and so forth are function expressions.
--
Rob
OK I'm pretty sure I've got it now
Thank you both for your patience and help
N
Can this function expression function as-is?
In other words, without appending the call operator or assigning it a
reference, how would one use it?
> // and
>
> var x = {}
> x.foo = function(){...}
>
> // and
>
> var y = {
> bar: function(){...}
> }
>
> </script>
>
> and so forth are function expressions.
--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Um, no, you cannot.
It should be noted that this creates a closure. Thusly it prevents your
code from being "poisoned." As well as your code not being able to
interfere with code outside its scope (that was created by the closure).