簡體   English   中英

為什么我的返回值變得未定義?

[英]Why does my return value become undefined?

我有這個功能:

function getStartDate(dateToTest) {
    var dateToUse;
    // The function here doesn't matter.  I get the same result even if I use "if (1 != 1)".
    if (!isAcceptableDate(dateToTest)) {
       dateToTest = dateToTest.addDays(1);
       getStartDate(dateToTest);
    }
    else {
        dateToUse = dateToTest;
        // It's not undefined here.......
        console.log(dateToUse);
       return dateToUse;
    }
}

此函數末尾的console.log中的dateToUse值很好。 但是,到了這個函數,突然就變成undefined了:

function getDates(startDate) {
    console.log(startDate);
    // Do a bunch of other stuff.
}

這是我如何稱呼這些東西:

var baseDate = new Date();
var adjustDate = getStartDate(baseDate);
var dateArray = getDates(adjustDate);

為什么在一個函數末尾具有完全有效值的變量在被另一個函數調用時會丟失其值?

getStartDate函數之前添加return關鍵字,如

     return getStartDate(dateToTest);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM