Skip to content

Commit d115f0c

Browse files
committed
Add "Functions/Recursive Functions" recipe.
1 parent a7787f4 commit d115f0c

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
layout: recipe
3+
title: Recursive Functions
4+
chapter: Functions
5+
---
6+
7+
h2. Problem
8+
9+
You want to call a function from within that same function.
10+
11+
h2. Solution
12+
13+
With a named function:
14+
15+
{% highlight coffeescript %}
16+
ping = ->
17+
console.log "Pinged"
18+
setTimeout ping, 1000
19+
{% endhighlight %}
20+
21+
With an unnamed function, using @arguments.callee@:
22+
23+
{% highlight coffeescript %}
24+
delay = 1000
25+
26+
setTimeout((->
27+
console.log "Pinged"
28+
setTimeout arguments.callee, delay
29+
), delay)
30+
{% endhighlight %}
31+
32+
h2. Discussion
33+

wanted-recipes.textile

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,6 @@ foo 1, 2, 3
9494
{% endhighlight %}
9595

9696
* Variable arguments with splats
97-
* Recursion of unnamed functions with arguments.callee
98-
99-
{% highlight coffeescript %}
100-
console.log [1,2,3,4,5].map (x) ->
101-
if x <= 1 then return 1
102-
return x * arguments.callee(x-1)
103-
# => [1, 2, 6, 24, 120 ]
104-
{% endhighlight %}
10597

10698
h2. jQuery
10799

0 commit comments

Comments
 (0)