473,395 Members | 1,666 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,395 software developers and data experts.

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
Jun 27 '08 #1
8 1097
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Vincent van Beveren | last post by:
Hi everyone, I have the following code using inheritence The important part is the function getName(). The result should alert 'John Doe': function First(name) { this.name = name; ...
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
34
by: electrician | last post by:
Perl has it, Basic has it, Fortran has it. What is so difficult about creating a goto command for JavaScript. Just set up a label and say go to it.
16
by: fabio | last post by:
it's bad to make a thing like: //declaration of variables //some code... n = inputdata int array;
20
by: Mike | last post by:
Hi all, I am cramming for an technical interview about C++ and programming... I believe there will be problems on data-structures and algorithms... could anybody recommend some online...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
22
by: Christopher Nelson | last post by:
I have a little menu system which essentially takes HTML like: <div id='foo'></div> and retrieves foo.shtml from the server and inserts it inside the <div>. But sometimes I'd like foo.shtml...
6
by: java | last post by:
Consider: var x = {a:3, b:x}; alert(x.b) ==undefined Question: why ? x is a pointer to an object, so x.b should refer to that same object.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.