Nameless JavaScript Function

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Nameless JavaScript Function

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Nameless JavaScript Function

Post 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); 
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: Nameless JavaScript Function

Post by anjanesh »

Apparently, this works if placed in eval()

Code: Select all

eval
 (
 function(a,b,c)
  {
         alert(a + b + c);
  }('a', 123, 'x')
 );
 
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Nameless JavaScript Function

Post 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]
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: Nameless JavaScript Function

Post by anjanesh »

It doesnt - also no error or even warning in the JS console.
Using FireFox 2.0.0.11
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Nameless JavaScript Function

Post 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]
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Nameless JavaScript Function

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Nameless JavaScript Function

Post 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?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Nameless JavaScript Function

Post 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]
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Nameless JavaScript Function

Post 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 :)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply