我们有这样一个JavaScript函数声明:
function itemIncludesTotal(item){
var includeItem = false;
if (item.lineItem.hasOwnProperty("details")
&& item.lineItem.details.hasOwnProperty("total")
&& item.lineItem.details["total"]
&& !item.lineItem.restricted
){
includeItem = true;
}
return includeItem;
};其中函数的参数(item)为:
var item = {lineItem: {details: {total: "$5.00"}, restricted: true}};函数调用和返回值为:
itemIncludesTotal(item); => TRUE || FALSE我们的小组正在讨论其结构:
发布于 2016-10-20 08:20:28
如果数据像问题中所示的那样简单明了,我就会琐碎地实现一些琐碎的事情。
function includesTotal(item) {
return item.lineItem.details
&& item.lineItem.details.total
&& !item.lineItem.restricted ? true : false;
}最后一个? true : false只是简单地表明返回值是布尔值。
但是,对于泛型函数,我会完整地检查路径:
function includesTotal(item) {
return item
&& item.lineItem
&& item.lineItem.details
&& item.lineItem.details.total
&& !item.lineItem.restricted ? true : false;
}https://codereview.stackexchange.com/questions/144708
复制相似问题