我有这样的声明:
pn = r.notFound == "true" ? "Not currently assigned" : "Currently assigned to VPMO " + r.currentAssignment;
我需要添加“未定义”的条件,然后为其添加一个值。它基本上需要阅读类似...
r.notFound == "undefined" then “Already assigned to this project”<br/>
else if
r.notFound == “true” then “Not currently assigned”<br/>
else<br/>
“Currently assigned to VPMO “ + r.currentAssignment;
最佳答案
var pn;
if (r.notFound == 'undefined') { // be aware that this checks for STRING undefined
pn = 'Already assigned to this project';
} else if (r.notFound == 'true') { // be aware that this checks for STRING true
pn = 'Not currently assigned';
} else {
pn = 'Currently assigned to VPMO ' + r.currentAssignment;
}
?
编辑
如果您想检查变量是否已定义,请使用:
if (typeof r.notFound === 'undefined')
关于JavaScript 语句简写需要扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7099626/