Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

puzzling function constructor

0 views
Skip to first unread message

noddy

unread,
Jun 24, 2007, 9:34:43 PM6/24/07
to
I have come across this code in a js file.
It uses the function constructor in a way I have never seen.
Can someone explain what is happening here?
Thanks

(function(){
...lines of code...
} ) ();

Rod

unread,
Jun 24, 2007, 10:47:32 PM6/24/07
to
> } ) ();- Hide quoted text -
>
> - Show quoted text -

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.

RobG

unread,
Jun 24, 2007, 11:17:08 PM6/24/07
to
On Jun 25, 12:47 pm, Rod <noblethras...@gmail.com> wrote:
> On Jun 24, 8:34 pm, noddy <n...@toyland.com> wrote:
>
> > I have come across this code in a js file.
> > It uses the function constructor in a way I have never seen.
> > Can someone explain what is happening here?
> > Thanks
>
> > (function(){
> > ...lines of code...
>
> > } ) ();- Hide quoted text -
>
> > - Show quoted text -
>
> 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.

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

noddy

unread,
Jun 24, 2007, 11:39:07 PM6/24/07
to


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 ?

Rod

unread,
Jun 25, 2007, 12:10:17 AM6/25/07
to
> Rob- Hide quoted text -

>
> - Show quoted text -

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.

noddy

unread,
Jun 25, 2007, 12:18:21 AM6/25/07
to
On Sun, 24 Jun 2007 21:10:17 -0700, Rod <noblet...@gmail.com>
wrote:

So what you are saying is that

(function(){...})();
turns the anonymous function declaration function(){...};
into a function expression
right ?

Rod

unread,
Jun 25, 2007, 2:09:16 AM6/25/07
to
On Jun 24, 11:18 pm, noddy <n...@toyland.com> wrote:
> On Sun, 24 Jun 2007 21:10:17 -0700, Rod <noblethras...@gmail.com>
> right ?- Hide quoted text -

>
> - Show quoted text -

right.

RobG

unread,
Jun 25, 2007, 8:11:50 AM6/25/07
to
On Jun 25, 2:18 pm, noddy <n...@toyland.com> wrote:
> >On Jun 24, 10:17 pm, RobG <r...@iinet.net.au> wrote:
[...]

> >> 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(){...})();
[...]

>
> So what you are saying is that
>
> (function(){...})();
> turns the anonymous function declaration function(){...};
> into a function expression
> 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

noddy

unread,
Jun 25, 2007, 8:48:37 AM6/25/07
to
On Mon, 25 Jun 2007 16:18:21 +1200, noddy <no...@toyland.com> wrote:

OK I'm pretty sure I've got it now

Thank you both for your patience and help

N

-Lost

unread,
Jun 26, 2007, 10:48:52 PM6/26/07
to

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.

-Lost

unread,
Jul 3, 2007, 10:00:07 PM7/3/07
to
-Lost wrote:
>> (function(){...})
>
> 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?

Um, no, you cannot.

-Lost

unread,
Jul 3, 2007, 10:01:57 PM7/3/07
to

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

0 new messages