码迷,mamicode.com
首页 > Web开发 > 详细

JS笔记1——多了一个undefined是什么鬼?

时间:2016-09-12 18:36:52      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

练习输出一个a行b列表格时,多输出了一个undefined!

技术分享

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
</body>
<script>
	/**
	*@intro 函数table:输出a行b列的表格,内容为字符串c
	*@param a number 表示表格行数
	*@param b number 表示表格列数
	*@param c string 表示表格内容
	*/
	function table(a,b,c){
		document.write(‘<table border="1" style="border-collapse:collapse">‘);
	for(var i=0;i<a;i++){
		document.write(‘<tr>‘);
		for(var n=0;n<b;n++){
			document.write(‘<td>‘);
			document.write(c);
			document.write(‘</td>‘);
		}
		document.write(‘</tr>‘);
	}
	document.write(‘</table>‘);
}
	 table(5,4,‘hehe‘);                    //可以正常输出
	//document.write(table(5,4,‘hehe‘));          //末尾有一个undefined,原因???
	//document.write(document.write(2+‘<br>‘));   //双重输出会出错,会显示undefine
	//alert(alert(2));							  //双重输出会出错,会显示undefine
	
</script>
</html>

  问题在于多写了最外层的document.write();去掉之后变得正常~

之后测试document.write(document.write(2))时,也出现了2换行undefined;alert也会。

可能是语法出错吧~输出的参数为输出函数本身,这好像的确不太合理~

 

JS笔记1——多了一个undefined是什么鬼?

标签:

原文地址:http://www.cnblogs.com/Christeen/p/5865447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!