javascript programming idiom

C

ctx2002

HI all:

I often see people write code like as below to invoke code block:

(
function() { do some thing here}
) ()

my question is why do like this, and what is benefit.

regards
 
S

Stevo

ctx2002 said:
HI all:
I often see people write code like as below to invoke code block:
(
function() { do some thing here}
) ()
my question is why do like this, and what is benefit.
regards

It can help keep the global namespace clean. It's defining an anonymouse
function and calling it immediately, and unless some kind of closure is
created, it will be discarded. All variables defined inside the function
are lost with it. For example, if you wanted to do this following code
INLINE:

var x=2;
var y=3;
var z=x*y;
alert(z);

You've multipled 2 by 3 and alerted 6. But behind you, you've left (and
possibly overwritten if they already existed) global variables x, y and
z. Where as this code below leaves/overwrites nothing:

(function(){var x=2;var y=3;var z=x*y;alert(z);})()

Same result, less pollution.

It can be just people showing off how many JS tricks they know though.
 
T

Thomas 'PointedEars' Lahn

ctx2002 said:
I often see people write code like as below to invoke code block:

(
function() { do some thing here}
) ()

my question is why do like this, and what is benefit.
^
My question -- which is actually a *question* -- is far more simple: Why
don't you use the newsgroup's archive, readily available through the Web
interface you are just bathing your hands in? This has been discussed ad
nauseam here already.

http://jibbering.com/faq/


PointedEars
 
C

ctx2002

hi Thomas:

can you be a bit nice to other people?
i have searched javascript FAQ, and did not found any answer.
not every one is a searching guru like you.

regards
 
D

dhtml

ctx2002 wrote:

Can you behave a bit less like a semi-literate idiot?

Part of the problem I have with these posts of yours is that it
discourages people from asking questions.

I have no doubt that you can be rude and obnoxious. Anyone can see
this by "searching the archives."

You've also got some pretty good knowledge and your responses can be
helpful.

In the OP's question, he might not know even what the term 'closure'
means or where to start with trying to figure out the coding construct
that he saw. In that case, you could point him to Richard's article on
Jibbering.

http://www.jibbering.com/faq/faq_notes/closures.html

Garrett
 
T

Thomas 'PointedEars' Lahn

dhtml said:
In the OP's question, he might not know even what the term 'closure'
means or where to start with trying to figure out the coding construct
that he saw. In that case, you could point him to Richard's article on
Jibbering.

http://www.jibbering.com/faq/faq_notes/closures.html

Or I could point them to the FAQ which refers to this article (at least it
should) and in addition provides a bunch of useful information on how to
improve his posting style, finally earning themselves the answers they
desire. BTDT.

They did not even consider to copy-paste and experiment with the code.
Nuff said.


F'up2 poster

PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <ee65eb53-8812-466c-8ad6-169af94c474d@p3
9g2000prm.googlegroups.com>, Thu, 29 May 2008 18:45:33, ctx2002
hi Thomas:

can you be a bit nice to other people?

The evidence is pretty clear : it is not a concept that he is capable of
understanding.
 
J

Jorge

In that case, you could point him to Richard's article on
Jibbering.

http://www.jibbering.com/faq/faq_notes/closures.html

This article has a typo in this paragraph :

The first object in the chain is ActInner1 and it has a property named
innerArg with the value 2. All of the other 3 identifiers correspond
with named properties of ActOuter1; arg1 is 2, arg2 is 4 and localVar
is 8. The function call returns ((2 + 2)/(2 + 8)).

Tha last sentence should read : "The function call returns ((2 + 4)/(2
+ 8))."

--Jorge.
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top