為什麼這樣寫就會變成undefined,return是什麼魔力看不明白
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函数</title>
<script type="text/javascript">
function com(a,b) {
if (a>b) {
document.write(a);
} else if (b>a) {
document.write(b);
} else {
document.write("兩者一樣大喔");
}
}
res1 = com(5,4)
res2 = com(6,3)
document.write(" 5 和 4 的较大值是:"+res1+"<br>");
document.write(" 6 和 3 的较大值是:"+res2 );
</script>
</head>
<body>
</body>
</html>你的com函数没有return,就没有返回值,就无法赋值给res1,你判断语句只是让屏幕输出什么,只是一个行为,是没有返回值的,还有 res1 前加var