为什么这个评估不起作用?

时间:2013-07-23 17:29:03

标签: javascript eval

当我尝试使用undefined运行时,以下动态生成的javascript正在返回eval()。如果我删除if/else语句,则会返回0,但当我尝试使用if/else语句时,它会返回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());

谁能告诉我为什么?

2 个答案:

答案 0 :(得分:4)

您将变量设置为SupportFullSupportNone,这些都是未定义的。

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

答案 1 :(得分:2)

查看您的代码:

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

但您刚刚声明SupportFullSupportNone,并没有为其指定任何值。

查看ECMAScript specs

  

未定义的值

     

当变量尚未赋值时使用的原始值