簡體   English   中英

為什么此javascript函數返回undefined?

[英]Why is this javascript function returning undefined ?

let getLowerUpperBoundFromValue=(bound , idToValue)=>{
        // Returns the value of the variable previously generated if "value" is a variable name  or return "value" if its a number . 
        // bound :  can be a direct integer or a variable name. 
        // idToValue : contains the id to value mapping which must contain a variable whose name must be equal to 'bound' parameter if its a variable name . 

        if(isNaN(Number(bound)))
        {
            Object.entries(idToVarStatesGlobal).forEach(idStatePair=>{
                let id= idStatePair[0] , varState = idStatePair[1] ; 
                if(varState.name===bound){
                    console.log("check now Returning idTovalue[id]" , idToValue , id , idToValue[id] , Number(idToValue[id]));
                    return Number(idToValue[id]) ; 
                }
            })
        }
        else return Number(bound); 
    }

當我做這樣的控制台日志時:

console.log('check now: ' , getLowerUpperBoundFromValue(varState.lowerbound , idToValue)) ; 

我得到這樣的日志輸出:

check now Returning idTovalue[id] {PSfCL5hBm: 69} PSfCL5hBm 69 69
inputGeneration.js:99 check now:  undefined

為什么即使Number(idTovalue[id])值返回到正常值69,該函數也仍返回未定義狀態?

什么也沒有返回,因為forEach回調是一個單獨的方法。 我刪除了對.forEach的調用,並用for of循環替換了它,該循環維護了return范圍

let getLowerUpperBoundFromValue=(bound , idToValue)=>{
        // Returns the value of the variable previously generated if "value" is a variable name  or return "value" if its a number . 
        // bound :  can be a direct integer or a variable name. 
        // idToValue : contains the id to value mapping which must contain a variable whose name must be equal to 'bound' parameter if its a variable name . 

        if(isNaN(Number(bound)))
        {
            for (let idStatePair of Object.entries(idToVarStatesGlobal)) {
                let id= idStatePair[0] , varState = idStatePair[1] ; 
                if(varState.name===bound){
                    console.log("check now Returning idTovalue[id]" , idToValue , id , idToValue[id] , Number(idToValue[id]));
                    return Number(idToValue[id]) ; 
                }
            }
        }
        else return Number(bound); 
    }

暫無
暫無

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

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