Home > Web Front-end > JS Tutorial > body text

Detailed explanation of code cases for anonymous recursive implementation in Javascript

黄舟
Release: 2017-05-26 10:01:57
Original
1231 people have browsed it

This article mainly introduces the example code of Javascript to implement anonymous recursion, and uses arguments.callee to implement anonymous recursion. Those who are interested can learn about

Recursion is a common programming technique. I believe everyone is familiar with real-name recursion, but what if you want to implement anonymous recursion? For example, if you want to return an anonymous recursive function, or define an anonymous recursive function and call it directly, how should you do it? This article will explore its implementation.

Real-name recursion

Let’s start with real-name recursion, or use the simplest example of finding factorial:

function fact(n) {
 if (n < 2) {
  return n;
 } else {
  return n * fact(n - 1);
 }
}
console.log(fact(5));
Copy after login

Recursion requirements Call yourself, which is too easy if the function has a name.

Use variables to achieve recursion

The function can also be assigned to a variable, but to achieve recursion, the function body still depends on thisVariable name:

var f = function(n) {
 if (n < 2) {
  return n;
 } else {
  return n * f(n - 1);
 }
}
console.log(f(5));
Copy after login

It should be said that this method is not essentially different from the previous one.

Anonymous Recursion

Now let’s discuss the implementation of anonymous recursion.

Initial idea

If you want to return an anonymous recursive function, or define an anonymous recursive function and call it directly:

(function (n) {
 if (n < 2) {
  return n;
 } else {
  return n * ?(n - 1);
 }
})(5);
Copy after login

If Without a name, we don't know what to fill in the question mark in the code, and we can't form recursion. What should we do at this time? At this time, it is necessary to ask for arguments object.

arguments object

In a javascript function, the arguments object represents the parameter object when actually called. In our recursive function, we actually don’t need to define the “formal parameters” n at all:

function factNoParam() {
 if (arguments[0] < 2) {
  return arguments[0];
 } else {
  return arguments[0] * factNoParam(arguments[0] - 1);
 }
}
console.log(factNoParam(5));
Copy after login

As long as we pass in the actual parameters when calling, we can use arguments[0] to get the actual parameters The value of this parameter passed in.

If there are more parameters, they can also be obtained by arguments[1], arguments[2], etc.

arguments.callee Attributes

arguments can be used to obtain parameters. I believe you may already know this, but the arguments object actually has There is a property called callee. arguments.callee represents the function itself. What does it mean? In fact, we can write fact like this:

function fact(n) {
 if (n < 2) {
  return n;
 } else {
  return n * arguments.callee(n - 1);
 }
}
console.log(fact(5));
Copy after login

Then it is still recursive. Because arguments.callee is actually equal to fact.

So, here we are, with the help of this attribute, it is not difficult to implement anonymous recursion, just change ? to arguments.callee:

(function (n) {
 if (n < 2) {
  return n;
 } else {
  return n * arguments.callee(n - 1);
 }
})(5);
Copy after login

If necessary, also This can be returned as anonymous recursion.

The above is the detailed content of Detailed explanation of code cases for anonymous recursive implementation in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!