Closed Thread Icon

Topic awaiting preservation: Recursive anonymous functions? Pages that link to <a href="https://ozoneasylum.com/backlink?for=25788" title="Pages that link to Topic awaiting preservation: Recursive anonymous functions?" rel="nofollow" >Topic awaiting preservation: Recursive anonymous functions?\

 
Author Thread
Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 05-17-2005 06:03

This question sounds a bit obtuse, but how would one create a recursive anonymous function in Javascript?

code:
(function (i) {
document.writeln(i);

if(i > 0)
/* SOMETHING!! */(i - 1);
})(10);



The only thing I can think of is this(i - 1);, however, this refers to the window object in this case.

I know it must be possible, I just can't figure out how.

---
Website

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 05-17-2005 06:58

Hmm, though it's not *exactly* what I was looking for, I found that I can do the following without needing an anonymous function.

code:
(function self(i) {
document.writeln(i);

if(i > 0)
self(i - 1);
})(10);



For some reason, I was thinking that the function needed to be anonymous in order to call it directly, but that's not the case.

*case closed*

---
Website

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 05-17-2005 07:36

The 2nd function is not really anonymous
To use this( i-1 ); in the first function I think something like this ;

code:
(new (function ( i )
{
document.writeln( i );
if( i>0 )
this( i-1 );
} ) )( 10 );

[edit] It doesn't work. I also thought to something along :

code:
new (function( j )
{
var
i = parseInt( arguments[0] ),
output = ""

if( i>0 )
{
output = (function()
{
document.writeln( i +"<br>" )
return output+this( i-1 )
})( i )
}
return output

})( 10 )

That is to create an anonymous Object with a(n anonymous) method being called when it is instanciated, but it doesn't work either.

There must be something like that on http://w3future.com/html/stories/

[/edit]





(Edited by poi on 05-17-2005 07:56)

TwoD
Bipolar (III) Inmate

From: Sweden
Insane since: Aug 2004

posted posted 05-17-2005 08:14
code:
(x=new Function("i","document.writeln(i);if(i > 0){x(i - 1)}"))(10);
document.write("<p>"+x)



code:
(x=function(i){
document.writeln(i);
if(i > 0)
x(i - 1);
})(10);
document.write("<p>"+x)


Also works, but then it doesn't say it's anonymous lol.

(Edited by TwoD on 05-17-2005 08:17)

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 05-17-2005 08:22

nice implementatio. Alas I doubt it still can be considered anonymous since the handle of the function is assigned to a variable.

TwoD
Bipolar (III) Inmate

From: Sweden
Insane since: Aug 2004

posted posted 05-17-2005 12:34

Well, the first function does say it's anonymous, or it's just been given that name.
Anyway, why not assign it to a variable if you're going to use the function more than once instead of forcing it to be anonymous and unassigned.

/TwoD

HZR
Paranoid (IV) Inmate

From: Cold Sweden
Insane since: Jul 2002

posted posted 05-17-2005 12:49
quote:
JavaScript: The Definitive Guide (111:1) says:
[...] the Arguments object defines a callee property that refers to the function that is currently being executed. This is useful, for example, to allow unnamed functions to invoke themselves recursively.


code:
(function(i) {
document.writeln(i);
if (i > 0) arguments.callee(i - 1);
})(10);



(Edited by HZR on 05-17-2005 13:07)

TwoD
Bipolar (III) Inmate

From: Sweden
Insane since: Aug 2004

posted posted 05-17-2005 14:43

Doh! I thought it was just callee... no wonder my first idea didn't work...
Note to self: Always check the reference when something doesn't work as planned!

/TwoD

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 05-17-2005 15:57

Yay!

Thanks, HZR.

---
Website

« BackwardsOnwards »

Show Forum Drop Down Menu