本文介绍了递归函数的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返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!