if條件判斷 里邊 return false; 或者return true;并不能作為函數(shù)的返回值。if條件判斷里的return,只是影響程序是否繼續(xù)執(zhí)行。
如果需要返回值,需要在函數(shù)層級 return 下
另外,可能牽扯到?jīng)]有涉及到的情況,需要有個默認(rèn)值。
<!DOCTYPE html>
<html lang="en">
<style>
div{
width: 100px;
height: 50px;
background-color: skyblue;
margin: 40px;
text-align: center;
line-height: 50px;
}
</style>
<body>
<div onclick="tag()">
按鈕
</div>
<script>
let aa = 'zifuchuanzifuchuan';
let bb = 'zifuchuanzifuchuan';
function tag(){
console.log('執(zhí)行了嗎')
console.log('函數(shù)執(zhí)行結(jié)果',compare(aa,bb)) //undefined
console.log('函數(shù)執(zhí)行結(jié)果',compareVal(aa,bb)) // true
if(!compare(aa,bb)){
return
}
console.log('還執(zhí)行嗎')
}
function compare(a,b){
if(a.length < b.length){
return false
}
}
function compareVal(a,b){
let compareFlag = true;
if(a.length < b.length){
compareFlag = false
}
return compareFlag
}
</script>
</body>
</html>