question on ways of loading a javascript function

Q

QQ

Hi all

could anyone here tell me the difference between
(function(){

})();

and

(function(){

)());

Thank you.
Steve
 
S

Stevo

QQ said:
Hi all

could anyone here tell me the difference between
(function(){

})();

and

(function(){

)());

Thank you.
Steve

The second one isn't valid. That's the difference. You can see the
parentheses/braces aren't paired correctly.
 
B

Bart Lateur

QQ said:
could anyone here tell me the difference between
(function(){

})();

and

(function(){

)());

The former works, and the latter is a syntax error.
 
Q

QQ

kangax, thank you very much for detailed explanation. appreciate
it :).

Also thanks other guys feedback as well,

The reference link is quite helpful :).


QQ said:
could anyone here tell me the difference between
(function(){



)());

There's not much of a difference. Both result in function being
interpreted as function expression, and invoked with empty arguments list..

In first case, function expression itself is surrounded with parenthesis
(grouping operator) and is then appended with arguments (last pair of
parens), resulting in object invocation.

Second expression is fully surrounded with parenthesis (grouping
operator), and itself consists of function expression, followed by
arguments (inner pair of parens). It also results in object invocation.

You can even omit grouping operator altogether (although, generally not
recommended, for the reasons of clarity), as long as invocation happens
within expression context:

1, function(){ return 'foo'; }();

or:

x = function(){ return 'foo'; }();

or (if part of another function):

return function(){ return 'foo'; }()

etc. But not:

function(){ return 'foo'; }();

since in this case it would be interpreted as a statement (and so result
in syntax error).

[...]
 
Q

QQ

(function(){}());

In addition to the comments of kangax, you may find
interesting the following April 10, 2006 thread
entitled: function syntax questionhttp://groups.google.com/group/comp.lang.javascript/browse_frm/thread...

Csaba Gabor from Vienna

That is interesting topic as well, greate resource to learn, thanks a
lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top