Javascript JS:制作一个更好的返回条件语句的函数
所以我希望这个函数返回条件语句。当没有提供参数时,它应该显示整个语句,而不显示任何数字(函数参数)。否则,将参数(数字)放入语句中。我的解决方案看起来并不优雅。我不知道您到底想实现什么,但有一点:Javascript JS:制作一个更好的返回条件语句的函数,javascript,function,conditional,Javascript,Function,Conditional,所以我希望这个函数返回条件语句。当没有提供参数时,它应该显示整个语句,而不显示任何数字(函数参数)。否则,将参数(数字)放入语句中。我的解决方案看起来并不优雅。我不知道您到底想实现什么,但有一点: function conditionForLinks(textNum, linkNum){ if (textNum == undefined || linkNum == undefined){ return "${typeof(conte
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+"}"
}
};
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+"}";
}
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('');
}