Home Web Front-end JS Tutorial Recursive implementation method in JS

Recursive implementation method in JS

Apr 18, 2018 am 09:07 AM
javascript accomplish method

This time I bring to you, what are the precautions, the following is a practical case, let's take a look.

Recursive function: A recursive function is formed by calling itself by name.

Recursively implement the factorial function:

Method 1: Use the name of the function through

 function factorial(num){
    if(num<=1){
      return 1;
    }else{
      return num*factorial(num-1);
    }
  }
  console.log(factorial(4));
Copy after login

The result is: 24;

However, there is a problem with this method of implementing recursion. Observe the following code:

function factorial(num){
    if(num<=1){
      return 1;
    }else{
      return num*factorial(num-1);
    }
  }
  var anthorFactorial=factorial;
  console.log(anthorFactorial(4));
Copy after login

The result is: 24;

But:

function factorial(num){
    if(num<=1){
      return 1;
    }else{
      return num*factorial(num-1);
    }
  }
  var anthorFactorial=factorial;
  factorial=null;
  console.log(anthorFactorial(4));
Copy after login

The result is: error

This is because:

The function name we defined is actually a pointer to the function, and the anotherFactorial we defined also points to that function, so calling anotherFactorial (4) can successfully output 24

When factorial = null, the reference of executing define function is left with anotherFactorial, then the above error message will be displayed when calling anotherFactorial(4).

At this time, arguments.callee can be used to replace factorial in the function definition.

Method 2: By using arguments.callee

function factorial(num){
    if(num<=1){
      return 1;
    }else{
      return num*arguments.callee(num-1);
    }
  }
  var anthorFactorial=factorial;
  factorial=null;
  console.log(anthorFactorial(4));
Copy after login

The result is: 24

arguments.callee is a pointer to the function being executed, so arguments.callee can be used to implement recursive calls to the function. By using arguments.callee instead of the function name, you can ensure that no problem will occur when calling the function. Therefore, when writing recursive functions, it is always safer to use arguments.callee than using function names.
However, in strict mode, arguments.callee cannot be accessed through scripts, and an error will be reported when accessing this property. However, the same effect can be achieved by naming function expressions.

Method 3: Expression through named function

 var factorial=function f(num){
    if(num<=1){
      return 1;
    }else{
      return num*f(num-1);
    }
  };
  f=null;
  console.log(factorial(4));
Copy after login

This approach works in both strict and non-strict modes.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:



The above is the detailed content of Recursive implementation method in JS. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel.

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones?

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed!

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

How to set font size on mobile phone (easily adjust font size on mobile phone)

The difference between Go language methods and functions and analysis of application scenarios The difference between Go language methods and functions and analysis of application scenarios Apr 04, 2024 am 09:24 AM

The difference between Go language methods and functions and analysis of application scenarios

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones

See all articles