0

The following dynamically generated javascript is returning undefined when I try to run it using eval(). If I remove the if/else statements, it returns 0 but when I try to use the if/else statements, it's returning undefined.

function doCalculation() {
    var OfferSupport=0;
    var DOOfferSupport=4;
    var SupportFull=SupportFull;
    var SupportNone=SupportNone;
    if(DOOfferSupport >= 1)
        OfferSupport = SupportFull ;
    else
        OfferSupport = SupportNone ;
    return OfferSupport;
}
console.log('test: ' +  doCalculation());

Can anyone tell me why?

4

2 回答 2

4

您将变量设置为SupportFullor SupportNone,它们都未定义。

如果你想取回一个值,你需要设置一个值。

于 2013-07-23T17:30:40.237 回答
2

查看您的代码:

var OfferSupport=0; 
var DOOfferSupport=4;
var SupportFull=SupportFull; 
var SupportNone=SupportNone;
if(DOOfferSupport >= 1) 
  OfferSupport = SupportFull ; 
else 
  OfferSupport = SupportNone ;

但是您刚刚声明了SupportFulland SupportNone,并没有为其分配任何值。

查看ECMAScript 规范

未定义的值

未为变量赋值时使用的原始值

于 2013-07-23T17:32:18.980 回答