Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 区分对递归函数的初始调用和后续调用_Javascript_Function_Recursion - Fatal编程技术网

Javascript 区分对递归函数的初始调用和后续调用

Javascript 区分对递归函数的初始调用和后续调用,javascript,function,recursion,Javascript,Function,Recursion,我的主要问题是,在JavaScript中区分递归函数的初始调用和后续调用的最简单方法是什么 让我举个例子 假设,如果在初始调用中传递给函数的字符串为空,我希望下面的函数返回false。有没有一种方法可以在不向函数中添加其他参数的情况下执行此操作 function isPalindrome(str) { if (str.length <= 1) { return true; } if (str.charAt(0) !== str.charAt(str.length

我的主要问题是,在JavaScript中区分递归函数的初始调用和后续调用的最简单方法是什么

让我举个例子

假设,如果在初始调用中传递给函数的字符串为空,我希望下面的函数返回false。有没有一种方法可以在不向函数中添加其他参数的情况下执行此操作

function isPalindrome(str) {
   if (str.length <= 1) {
     return true;
   }
   if (str.charAt(0) !== str.charAt(str.length -1)) {
     return false;
   }
   return isPalindrome(str.substr(1, str.length - 2));
}

isPalindrome('') // returns true, but I want this to return false

但我将其重新定义为递归函数,以解决更广泛的问题…

您可以有一个嵌套函数,即使具有相同的名称:

function isPalindrome(str) {

  // The code out here is for the initial call:
  if (str === '')
    return false;

  // Then do the recursive call
  return function isPalindrome(str) {
    // within this scope, isPalindrome refers to this function

    if (str.length <= 1) {
      return true;
    }
    if (str.charAt(0) !== str.charAt(str.length -1)) {
      return false;
    }
    return isPalindrome(str.substr(1, str.length - 2));
  }(str); // call this function immediately

}

即使使用相同的名称,也可以使用嵌套函数:

function isPalindrome(str) {

  // The code out here is for the initial call:
  if (str === '')
    return false;

  // Then do the recursive call
  return function isPalindrome(str) {
    // within this scope, isPalindrome refers to this function

    if (str.length <= 1) {
      return true;
    }
    if (str.charAt(0) !== str.charAt(str.length -1)) {
      return false;
    }
    return isPalindrome(str.substr(1, str.length - 2));
  }(str); // call this function immediately

}

即使使用相同的名称,也可以使用嵌套函数:

function isPalindrome(str) {

  // The code out here is for the initial call:
  if (str === '')
    return false;

  // Then do the recursive call
  return function isPalindrome(str) {
    // within this scope, isPalindrome refers to this function

    if (str.length <= 1) {
      return true;
    }
    if (str.charAt(0) !== str.charAt(str.length -1)) {
      return false;
    }
    return isPalindrome(str.substr(1, str.length - 2));
  }(str); // call this function immediately

}

即使使用相同的名称,也可以使用嵌套函数:

function isPalindrome(str) {

  // The code out here is for the initial call:
  if (str === '')
    return false;

  // Then do the recursive call
  return function isPalindrome(str) {
    // within this scope, isPalindrome refers to this function

    if (str.length <= 1) {
      return true;
    }
    if (str.charAt(0) !== str.charAt(str.length -1)) {
      return false;
    }
    return isPalindrome(str.substr(1, str.length - 2));
  }(str); // call this function immediately

}

不要试图区分不同的调用-函数的结果不应取决于副作用,也绝对不应取决于调用堆栈

相反,请使用第二个函数,它的作用稍有不同:

function isPalindrome(str) {
   return str.length <= 1 ||
     str.charAt(0) == str.charAt(str.length-1) && isPalindrome(str.slice(1, -1));
}
function isNonEmptyPalindrome(str) {
   return str.length > 0 && isPalindrome(str);
}
函数isAlindrome(str){
返回str.length 0&&isAlindrome(str);
}

不要试图区分不同的调用-函数的结果不应取决于副作用,也绝对不应取决于调用堆栈

相反,请使用第二个函数,它的作用稍有不同:

function isPalindrome(str) {
   return str.length <= 1 ||
     str.charAt(0) == str.charAt(str.length-1) && isPalindrome(str.slice(1, -1));
}
function isNonEmptyPalindrome(str) {
   return str.length > 0 && isPalindrome(str);
}
函数isAlindrome(str){
返回str.length 0&&isAlindrome(str);
}

不要试图区分不同的调用-函数的结果不应取决于副作用,也绝对不应取决于调用堆栈

相反,请使用第二个函数,它的作用稍有不同:

function isPalindrome(str) {
   return str.length <= 1 ||
     str.charAt(0) == str.charAt(str.length-1) && isPalindrome(str.slice(1, -1));
}
function isNonEmptyPalindrome(str) {
   return str.length > 0 && isPalindrome(str);
}
函数isAlindrome(str){
返回str.length 0&&isAlindrome(str);
}

不要试图区分不同的调用-函数的结果不应取决于副作用,也绝对不应取决于调用堆栈

相反,请使用第二个函数,它的作用稍有不同:

function isPalindrome(str) {
   return str.length <= 1 ||
     str.charAt(0) == str.charAt(str.length-1) && isPalindrome(str.slice(1, -1));
}
function isNonEmptyPalindrome(str) {
   return str.length > 0 && isPalindrome(str);
}
函数isAlindrome(str){
返回str.length 0&&isAlindrome(str);
}

这基本上与Bergi的答案相同,但在isPalindrome中声明了helper函数,因此它不会在其他地方使用

回文的一个更好的例子是,所有标点符号都应该删除,字母都应该大写或小写(这样比较就不区分大小写),但只能在第一次调用时进行。在那之后,比较角色就很简单了

length==zero部分也只处理一次,如果没有要比较的字符,则不会递归调用该函数

下面执行初始处理,然后调用一个内部函数

使用函数声明代替命名函数表达式,后者有命名函数表达式


这本质上与Bergi的答案相同,但在isAlindrome中声明了helper函数,因此它不会在其他地方使用

回文的一个更好的例子是,所有标点符号都应该删除,字母都应该大写或小写(这样比较就不区分大小写),但只能在第一次调用时进行。在那之后,比较角色就很简单了

length==zero部分也只处理一次,如果没有要比较的字符,则不会递归调用该函数

下面执行初始处理,然后调用一个内部函数

使用函数声明代替命名函数表达式,后者有命名函数表达式


这本质上与Bergi的答案相同,但在isAlindrome中声明了helper函数,因此它不会在其他地方使用

回文的一个更好的例子是,所有标点符号都应该删除,字母都应该大写或小写(这样比较就不区分大小写),但只能在第一次调用时进行。在那之后,比较角色就很简单了

length==zero部分也只处理一次,如果没有要比较的字符,则不会递归调用该函数

下面执行初始处理,然后调用一个内部函数

使用函数声明代替命名函数表达式,后者有命名函数表达式


这本质上与Bergi的答案相同,但在isAlindrome中声明了helper函数,因此它不会在其他地方使用

回文的一个更好的例子是,所有标点符号都应该删除,字母都应该大写或小写(这样比较就不区分大小写),但只能在第一次调用时进行。在那之后,比较角色就很简单了

length==zero部分也只处理一次,如果没有要比较的字符,则不会递归调用该函数

下面执行初始处理,然后调用一个内部函数

使用函数声明代替命名函数表达式,后者有命名函数表达式


仅在初始调用时传入第二个参数您可以使用
参数检查堆栈跟踪。被调用方
hack,但是。。。最好只添加一些参数。@Rooster-我认为最好在后续调用中传递第二个参数(这可能是一个计数器,用于说明递归有多深),而不是第一个参数。仅在初始调用中传递第二个参数您可以使用
参数检查堆栈跟踪。被调用方
hack,但是。。。最好只添加一些参数。@Rooster-我认为最好在后续调用中传递第二个参数(这可能是一个计数器,用于说明递归有多深),而不是第一个参数。仅在初始调用中传递第二个参数您可以使用
参数检查堆栈跟踪。被调用方
hack,但是。。。最好只添加一些参数。@Rooster-我认为最好在后续调用中传递第二个参数(这可能是一个计数器,用于说明递归有多深),而不是第一个参数。仅在初始调用中传递第二个参数您可以使用
参数检查堆栈跟踪。被调用方
hack,但是。。。更好的强制法