English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Analysis of the usage of arguments.callee in JavaScript functions

This example explains the usage of arguments.callee in JavaScript functions. Share it with everyone for reference, as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
  <script type="text/javascript">
    //method1, this method cannot implement recursive factorial after the function name fac points to a new function
//    function fac(num) {
//      if (num <= 1) {
//        return 1;
//      }
//      else {
//        return num * fac(num - 1);
//      }
    //method2
    function fac(num) {
      if (num <= 1) {
        return 1;
      }
      else {
        return num * arguments.callee(num - 1);  //arguments.callee represents the reference to the current method
      }
    }
    window.onload = function () {
      var func = fac;
      fac = function () {  //points to a new function
        return 1;
      }
      alert(func(5));  //, using method one will output5, using method two will output5factorial value
      alert(fac(5));   //Output1
    }
  </script>
</head>
<body>
</body>
</html>

Readers who are interested in more JavaScript-related content can check the special topics on this site: 'Summary of JavaScript Array Operation Techniques', 'Summary of JavaScript Mathematical Operation Usage', 'Summary of JavaScript Data Structure and Algorithm Techniques', 'Summary of JavaScript Switching Effects and Techniques', 'Summary of JavaScript Search Algorithm Techniques', 'Summary of JavaScript Animation Effects and Techniques', 'Summary of JavaScript Error and Debugging Techniques', and 'Summary of JavaScript Traversal Algorithm and Techniques'.

I hope the content described in this article will be helpful to everyone in programming JavaScript.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like