本文介绍了javascript返回递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
讨厌为前一个扩展程序打开一个新问题:
Hate to open a new question for an extension to the previous one:
function ctest() {
this.iteration = 0;
this.func1 = function() {
var result = func2.call(this, "haha");
alert(this.iteration + ":" + result);
}
var func2 = function(sWord) {
this.iteration++;
sWord = sWord + "lol";
if ( this.iteration < 5 ) {
func2.call(this, sWord);
} else {
return sWord;
}
}
}
这会返回迭代= 5但是结果未定?怎么可能?我明确地返回了sWord。它应该返回hahalollollollollol并且仅用于双重检查,如果我在返回sWord之前警告(sWord)它正确地显示它。
this returns iteration = 5 but result UNDEFINED ? how is that possible ? i explicitly return sWord. It should have returned "hahalollollollollol" and just for doublecheck, if i alert(sWord) just before the return sWord it displays it correctly.
推荐答案
你必须一直向上返回:
func2.call(this, sWord);
应为:
return func2.call(this, sWord);
这篇关于javascript返回递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!