2015-10-20 42 views
2
function conditionForLinks(textNum, linkNum){ 
      if (textNum == undefined || linkNum == undefined){ 
        return "${typeof(contentAsset.custom.brandInfoLinkUrl) !== 'undefined' && contentAsset.custom.brandInfoLinkUrl && typeof(contentAsset.custom.brandInfoLinkText) !== 'undefined' && contentAsset.custom.brandInfoLinkText}" 
       }else{ 
        return "${typeof(contentAsset.custom.brandInfoLinkUrl"+textNum+") !== 'undefined' && contentAsset.custom.brandInfoLinkUrl"+textNum+" && typeof(contentAsset.custom.brandInfoLinkText"+linkNum+") !== 'undefined' && contentAsset.custom.brandInfoLinkText"+textNum+"}" 
       } 
     }; 

所以我想这个函数返回条件语句。当没有提供参数时,它应该显示没有任何数字(函数参数)的整个语句.Else将参数(数字)放入语句中。我的解决方案看起来不够优雅。JS:制作更好的函数返回条件语句

回答

1

我不知道你想什么来完成,但这里的东西:

function conditionForLinks(textNum, linkNum){ 

    textNum = (textNum == null) ? "" : textNum; 
    linkNum = (linkNum == null) ? "" : linkNum; 

    return "${typeof(contentAsset.custom.brandInfoLinkUrl"+textNum+") !== 'undefined' && contentAsset.custom.brandInfoLinkUrl"+textNum+" && typeof(contentAsset.custom.brandInfoLinkText"+linkNum+") !== 'undefined' && contentAsset.custom.brandInfoLinkText"+textNum+"}"; 
} 
2
function conditionForLinks (textNum, linkNum) { 
    if(textNum == undefined || linkNum == undefined) { 
     textNum = ''; 
     linkNum = ''; 
    } 
    return ["${typeof(contentAsset.custom.brandInfoLinkUrl", textNum, ") !== 'undefined' && contentAsset.custom.brandInfoLinkUrl", textNum, " && typeof(contentAsset.custom.brandInfoLinkText", linkNum, ") !== 'undefined' && contentAsset.custom.brandInfoLinkText", textNum, "}"].join(''); 
}