Page 1 of 1

Nameless JavaScript Function

Posted: Fri Jan 18, 2008 8:25 am
by anjanesh
Hi

How does a function with no name work ? It seems its valid but how do we use it ?

Code: Select all

<script type="text/javascript">
function(a,b,c)
 {
        alert(a);
 }('g', 'b', 'c');
</script>
 
Thanks

Re: Nameless JavaScript Function

Posted: Fri Jan 18, 2008 8:53 am
by VladSun

Code: Select all

 var my_func = function(a,b,c){        alert(a);} my_func(1, 2, 3); 
It's often used for event handling or for object method implementing (you can use the keyword this to reference the object itself). I.e.:

Code: Select all

 window.onload = my_func; window.prototype.my_new_window_method = my_func;window.my_new_window_method(1, 2, 3); 

Re: Nameless JavaScript Function

Posted: Fri Jan 18, 2008 9:03 am
by anjanesh
Apparently, this works if placed in eval()

Code: Select all

eval
 (
 function(a,b,c)
  {
         alert(a + b + c);
  }('a', 123, 'x')
 );
 

Re: Nameless JavaScript Function

Posted: Fri Jan 18, 2008 9:30 am
by Weirdan
anjanesh wrote:Apparently, this works if placed in eval()

Code: Select all

eval
 (
 function(a,b,c)
  {
         alert(a + b + c);
  }('a', 123, 'x')
 );
 
This should work even if it's not placed in eval:
[js] function (a, b, c) {   alert(a + b + c);} ('a', 123, 'x'); [/js]

Re: Nameless JavaScript Function

Posted: Fri Jan 18, 2008 9:42 am
by anjanesh
It doesnt - also no error or even warning in the JS console.
Using FireFox 2.0.0.11

Re: Nameless JavaScript Function

Posted: Fri Jan 18, 2008 10:13 am
by Weirdan
yes, it should look like this to work without eval:
[js] (function (a, b, c) {   alert(a + b + c);}) (1,2,3); [/js]

Re: Nameless JavaScript Function

Posted: Fri Jan 18, 2008 12:52 pm
by VladSun
anjanesh wrote:Apparently, this works if placed in eval()

Code: Select all

eval
 (
 function(a,b,c)
  {
         alert(a + b + c);
  }('a', 123, 'x')
 );
 
Nope - what happens in this case indeed, is that you execute the function as an argument of the eval(). But this function returns nothing so your eval() doesn't evaluate anything.

Re: Nameless JavaScript Function

Posted: Fri Jan 18, 2008 12:53 pm
by VladSun
anjanesh wrote:Hi

How does a function with no name work ? It seems its valid but how do we use it ?

Code: Select all

<script type="text/javascript">
function(a,b,c)
 {
        alert(a);
 }('g', 'b', 'c');
</script>
 
Thanks
This usage makes no sense to me. Why do you have to define an in-line function which can be used just once?

Re: Nameless JavaScript Function

Posted: Fri Jan 18, 2008 12:59 pm
by Weirdan
VladSun wrote:Why do you have to define an in-line function which can be used just once?
Once-only function could be useful to introduce additional scope. But they usually look like this:
[js] (function() {   var a = 2;   // do something to a;})();// here a is not defined [/js]

Re: Nameless JavaScript Function

Posted: Fri Jan 18, 2008 1:04 pm
by VladSun
Weirdan wrote:Once-only function could be useful to introduce additional scope.
I don't think it worths it :)
Hm ... I was almost sure that:

[js]{    var a= 10;}alert(a); [/js]
would do it - but it doesn't :)

EDIT: Well, I must agree that it could be useful for JS libraries :)