如果/ Else声明未评估

时间:2017-02-27 00:16:40

标签: javascript if-statement

我有一个小帮手功能,不评估If或Else。

我知道正在调用该函数,因为我有nlapiLogExecution,这是你在NetSuite中调试的方式。有关记录内容的注释与代码有关。

这怎么可能?我也试过使用==运算符。我也尝试将其设置为函数内部的变量(我认为这是不必要的)。

function convertUnit(unit, cubicMeters){
    nlapiLogExecution('DEBUG','convertUnitFunction',typeof(unit))
    // typeof is String
    nlapiLogExecution('DEBUG','convertUnitFunction',unit)
    // value is Each
    if (unit === 'Each'){
        return cubicMeters
        nlapiLogExecution('DEBUG','equals Each', cubicMeters)
        // does not log here
    }
    else {
        nlapiLogExecution('DEBUG','else statements', 'equals else')
        // Does not log here
    }
}

2 个答案:

答案 0 :(得分:3)

您要输入if语句,但输入return函数才能记录任何内容。尝试切换return和日志语句的顺序:

function convertUnit(unit, cubicMeters){
    nlapiLogExecution('DEBUG','convertUnitFunction',typeof(unit))
    // typeof is String
    nlapiLogExecution('DEBUG','convertUnitFunction',unit)
    // value is Each
    if (unit === 'Each'){
        nlapiLogExecution('DEBUG','equals Each', cubicMeters)
        // will log something now if you pass 'Each'
        return cubicMeters
    }
    else {
        nlapiLogExecution('DEBUG','else statements', 'equals else')
        // will log something if the else branch is taken
    }
}

答案 1 :(得分:0)

您在到达nlapiLogExecution部分的'Each'之前返回。不知道为什么它不在其他地方运行,除非你只是传递'Each'