By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,401 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,401 IT Pros & Developers. It's quick & easy.

javascript programming idiom

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
ctx2002 wrote:
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.
Jun 27 '08 #2

P: n/a
ctx2002 wrote:
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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jun 27 '08 #3

P: n/a
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

On May 30, 12:40*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
ctx2002 wrote:
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
--
* * realism: * *HTML 4.01 Strict
* * evangelism: XHTML 1.0 Strict
* * madness: * *XHTML 1.1 as application/xhtml+xml
* * * * * * * * * * * * * * * * * * * * * * * * * * -- Bjoern Hoehrmann
Jun 27 '08 #4

P: n/a
ctx2002 wrote:
hi Thomas:
This is NetNews, not private e-mail.
can you be a bit nice to other people?
Can you behave a bit less like a semi-literate idiot?

http://www.catb.org/~esr/faqs/smart-questions.html
[snipped top post]
Score adjusted

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jun 27 '08 #5

P: n/a
On May 30, 4:18*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
ctx2002 wrote:
can you be a bit nice to other people?

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

PointedEars
Jun 27 '08 #6

P: n/a
dhtml wrote:
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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #7

P: n/a
In comp.lang.javascript message <ee65eb53-8812-466c-8ad6-169af94c474d@p3
9g2000prm.googlegroups.com>, Thu, 29 May 2008 18:45:33, ctx2002
<ct*****@gmail.composted:
>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.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Jun 27 '08 #8

P: n/a
On May 30, 11:43*pm, dhtml <dhtmlkitc...@gmail.comwrote:
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.
Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.