Calling a function when it's defined

S

Sam Kong

Hi,

I want to define anonymous functions which are called as soon as they
are defined.
My goal is to make a local scope.
So I don't need to give functions a name.

[1]
(function() {
//some code here.
})();

This works fine.
But the parens around the function look weird.

I realized that the following code does the same thing.

[2]
void function() {
//some code here.
}();

It's longer but looks better to me.

My questions are...

Is [2] a right and reliable way?
If so, why does it work that way?
If not, why not?

Thanks.

Sam
 

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
474,160
Messages
2,570,890
Members
47,423
Latest member
henerygril

Latest Threads

Top