473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

puzzling function constructor

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...
} ) ();
Jun 25 '07 #1
11 1309
Rod
On Jun 24, 8:34 pm, noddy <n...@toyland.comwrote:
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. 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.

Jun 25 '07 #2
On Jun 25, 12:47 pm, Rod <noblethras...@gmail.comwrote:
On Jun 24, 8:34 pm, noddy <n...@toyland.comwrote:
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

Jun 25 '07 #3
On Sun, 24 Jun 2007 20:17:08 -0700, RobG <rg***@iinet.net.auwrote:
>On Jun 25, 12:47 pm, Rod <noblethras...@gmail.comwrote:
>On Jun 24, 8:34 pm, noddy <n...@toyland.comwrote:
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.

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 ?
Jun 25 '07 #4
Rod
On Jun 24, 10:17 pm, RobG <r...@iinet.net.auwrote:
On Jun 25, 12:47 pm, Rod <noblethras...@gmail.comwrote:


On Jun 24, 8:34 pm, noddy <n...@toyland.comwrote:
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- 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.

Jun 25 '07 #5
On Sun, 24 Jun 2007 21:10:17 -0700, Rod <no***********@gmail.com>
wrote:
>On Jun 24, 10:17 pm, RobG <r...@iinet.net.auwrote:
>On Jun 25, 12:47 pm, Rod <noblethras...@gmail.comwrote:


On Jun 24, 8:34 pm, noddy <n...@toyland.comwrote:
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- 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.
So what you are saying is that

(function(){...})();
turns the anonymous function declaration function(){...};
into a function expression
right ?
Jun 25 '07 #6
Rod
On Jun 24, 11:18 pm, noddy <n...@toyland.comwrote:
On Sun, 24 Jun 2007 21:10:17 -0700, Rod <noblethras...@gmail.com>
wrote:


On Jun 24, 10:17 pm, RobG <r...@iinet.net.auwrote:
On Jun 25, 12:47 pm, Rod <noblethras...@gmail.comwrote:
On Jun 24, 8:34 pm, noddy <n...@toyland.comwrote:
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- 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.

So what you are saying is that

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

- Show quoted text -
right.

Jun 25 '07 #7
On Jun 25, 2:18 pm, noddy <n...@toyland.comwrote:
On Jun 24, 10:17 pm, RobG <r...@iinet.net.auwrote:
[...]
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

Jun 25 '07 #8
On Mon, 25 Jun 2007 16:18:21 +1200, noddy <no***@toyland.comwrote:

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

Thank you both for your patience and help

N
Jun 25 '07 #9
RobG wrote:
On Jun 25, 2:18 pm, noddy <n...@toyland.comwrote:
>>On Jun 24, 10:17 pm, RobG <r...@iinet.net.auwrote:
[...]
>>>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(){...})
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.
Jun 27 '07 #10
-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
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jul 4 '07 #11
noddy 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...
} ) ();
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).

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jul 4 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: penny336 | last post by:
dear all, i wrote a class integerSet which contain element as integer here is the driver program main IntegerSet a( n ); a.inputSet(); IntegerSet b (a); IntegerSet c(2*n); c =...
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
5
by: bipod.rafique | last post by:
Hello All, I am little confused about the constructor function. Say I have a class like the following class test{ int a; public: test(){}
6
by: Alexis Nikichine | last post by:
Hello, Today, I have a function: function f() { } and am looking for a way of distinguishing, from inside f, whether it
41
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum +=...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
14
by: Nickolay Ponomarev | last post by:
Hi, Why does http://www.jibbering.com/faq/ uses new Function constructor instead of function expressions (function(...) { ... }) when defining new functions? E.g. for LTrim and toFixed. Is the...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.