JavaScript and client side scripting.
Moderator: General Moderators
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Fri Jan 18, 2008 8:25 am
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
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Fri Jan 18, 2008 8:53 am
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
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Fri Jan 18, 2008 9:03 am
Apparently, this works if placed in eval()
Code: Select all
eval
(
function(a,b,c)
{
alert(a + b + c);
}('a', 123, 'x')
);
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Fri Jan 18, 2008 9:30 am
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]
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Fri Jan 18, 2008 9:42 am
It doesnt - also no error or even warning in the JS console.
Using FireFox 2.0.0.11
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Fri Jan 18, 2008 10:13 am
yes, it should look like this to work without eval:
[js] (function (a, b, c) { alert(a + b + c);}) (1,2,3); [/js]
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Fri Jan 18, 2008 12:52 pm
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
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Fri Jan 18, 2008 12:53 pm
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
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Fri Jan 18, 2008 12:59 pm
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]
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Fri Jan 18, 2008 1:04 pm
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