JInt(Javascript .NET 解释器)在公式中包含多语句

JInt (Javascript interpreter for .NET) with multistatement in Formula

我尝试使用具有简单表达式的 JInt(Javascript .NET 解释器):

var engineTest = new Engine ()
    .SetValue ("X", 10.1)
    .SetValue ("Y", 20.5)
    .SetValue ("Code", "A");
    var dFormula = @"if (Code === 'A'){X+Y;} if (Code === 'B'){Y-X;}";
var result = engineTest.Execute(dFormula).GetCompletionValue();

此公式的结果将为“undefined”。如果我将 dFormula 更改为

var dFormula = @"if (Code === 'A'){X+Y;}";

var dFormula = @"if (Code === 'A'){X+Y;} else if (Code === 'B'){Y-X;}";

结果将是正确的。 JInt (2.5.0) 有什么问题。 或者它可能不支持公式中的多个语句?我试图用“{}”括号包裹公式,但没有结果。

原因是: 在执行每个语句之前,JInt 会重置 Complition 值。因此,此公式仅在代码 === "B" 时有效,否则结果将被 'undefined' 值覆盖。

有两种修复方法:

  1. 合并成单个语句(使用 else)。
  2. 添加到公式输出变量,如

    var dFormula = @"if (Code === 'A'){RESULT = X+Y;} if (Code === 'B'){RESULT = Y-X;}";

为什么不用return:

if (Code === 'A') return X+Y;
if (Code === 'B') return Y-X;

return Code === 'A' ? X+Y : (Code === 'B' ? Y-X : undefined);

或使用开关

switch (Code) {
    case a:
...