問什么出現(xiàn)undefined?
代碼如下: <!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/> <title>函數(shù)傳參</title> <script?type="text/JavaScript"> ???function?add3(x,y,z)??????????????? ????{ ??????sum?=?x?+?y?+z; ??????document.write(x+"、"+y+"、"+z+"和:"+sum+"<br/>"); ????} ????document.write(add3(5,8,3)); ????document.write(add3(7,1,4)); ????? </script> </head> <body> </body> </html>
結(jié)果:
5、8、3和:16
undefined7、1、4和:12
undefined
為什么出現(xiàn)undefined?
2016-02-09
調(diào)用的時候,不要使用document.write(add3(5,8,3)),直接add3(5,8,3)。
2016-04-24
docment.write() 這個輸出的是函數(shù)的返回值,這個函數(shù)沒有返回sum的值,所以為空,前面會輸出是因為函數(shù)體里有docment.write()!
在這里例子里,寫一句函數(shù)里寫上return(sum),就會顯示兩次結(jié)果,結(jié)果如下:
5、8、3和:16
16
7、1、4和:12
12
把函數(shù)體里的document.write(x+"、"+y+"、"+z+"和:"+sum+"<br/>"); 刪除,就只顯示結(jié)果。
如果要顯示5、8、3和:16這種,我想你應(yīng)該知道怎么寫了!
2016-02-09
知道了!非常感謝!