f



javascript programming idiom

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
0
ctx2002
5/29/2008 11:30:19 PM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

8 Replies
583 Views

Similar Articles

[PageSpeed] 19

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.
0
Stevo
5/30/2008 12:19:59 AM
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
0
Thomas
5/30/2008 12:40:38 AM
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=A0pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> ctx2002 wrote:
> > I often see people write code like as below to invoke code block:
>
> > (
> > =A0function() { do some thing here}
> > ) ()
>
> > my question is why do like this, and what is benefit.
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ^
> 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? =A0This has been discussed a=
d
> nauseam here already.
>
> http://jibbering.com/faq/
>
> PointedEars
> --
> =A0 =A0 realism: =A0 =A0HTML 4.01 Strict
> =A0 =A0 evangelism: XHTML 1.0 Strict
> =A0 =A0 madness: =A0 =A0XHTML 1.1 as application/xhtml+xml
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- Bjoern Hoehrmann

0
ctx2002
5/30/2008 1:45:33 AM
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, <f806at$ail$1$8300dec7@news.demon.co.uk>
0
Thomas
5/30/2008 11:18:53 AM
On May 30, 4:18=A0am, 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
0
dhtml
5/30/2008 9:43:44 PM
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>
0
Thomas
5/30/2008 10:47:13 PM
In comp.lang.javascript message <ee65eb53-8812-466c-8ad6-169af94c474d@p3
9g2000prm.googlegroups.com>, Thu, 29 May 2008 18:45:33, ctx2002
<ctx2002@gmail.com> posted:
>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)
0
Dr
5/31/2008 6:55:46 PM
On May 30, 11:43=A0pm, dhtml <dhtmlkitc...@gmail.com> wrote:
> 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.
0
Jorge
6/1/2008 8:02:40 AM
Reply: