<!DOCTYPE?html>
<html?xmlns="http://www.w3.org/1999/xhtml">
<head>
????<title></title>
????<script?type="text/javascript">
?????????var?msg="\n函數(shù)的遞歸調(diào)用:\n\n";
?????????//響應(yīng)按鈕的點(diǎn)擊事件
?????????function?Test()
?????????{
?????????????var?result;
?????????????msg+="調(diào)用語(yǔ)句:\n";
?????????????msg+="????result=sum(20);\n";
?????????????msg+="調(diào)用步驟:\n";
?????????????result=sum(20);
?????????????msg+="計(jì)算結(jié)果:\n";
?????????????msg+="????result="+result+"\n";
?????????????alert(msg);
?????????}
?????????//計(jì)算當(dāng)前步驟加和值
?????????function?sum(m)
?????????{
?????????????if(m==0)
?????????????{
?????????????????return?0;
?????????????}
?????????????else
?????????????{
?????????????????msg+="????result="+m+"+sum("+(m-2)+?");\n";
?????????????????result=m+sum(m-2);
?????????????}
?????????????return?result;
?????????}
????</script>
</head>
<body>
????<input?type="button"?value="測(cè)試"?onclick="Test()"/>
</body>
</html>這個(gè)遞歸函數(shù)大體的意思我能明白,它在自身中調(diào)用。我有一點(diǎn)不明白,這里的"msg+="調(diào)用語(yǔ)句:\n"。a+=b不是賦值運(yùn)算嗎?一開始已經(jīng)定義了"var msg="函數(shù)的遞歸調(diào)用:",那么"msg+="調(diào)用語(yǔ)句"應(yīng)該等價(jià)于"msg=msg+"調(diào)用語(yǔ)句",即"函數(shù)的遞歸調(diào)用:調(diào)用語(yǔ)句"。但是提交后顯示的只是"調(diào)用語(yǔ)句"。如圖求各位老師解惑。
JS的遞歸函數(shù)疑問(wèn)
天窗的螞蟻
2016-01-22 11:40:22