wenLiangcan
wenLiangcan

Reputation: 109

How to write recursive function in MoonScript?

Is there something like arguments.callee of JavaScript for MoonScript?

Upvotes: 1

Views: 194

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26764

Since Moonscript functions are defined as local func; func = function() end, they are all recursive. This will print 120:

recursive = (n) -> return n > 1 and n*recursive(n-1) or 1
print recursive 5

As far as I know, there is no arguments.calee alternative, but I haven't seen cases where I'd need it either. Even Mozilla's docs say "there are nearly no cases where the same result cannot be achieved with named function expressions" about arguments.callee.

Upvotes: 5

Related Questions