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
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