您的位置:首页 > Web前端 > JavaScript

JavaScript中函数定义的三种方式

2015-08-25 09:32 585 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
	<script>
		/*
		 *函数定义的一种方式	
		*/
		function aa(){
			document.write("aaaa");
		}
		
	</script>
	<!--<button name="hanshu" onclick="aa()">按钮</button> -->
	<input type="button" value="按钮" onclick="aa()" />
	<script>
		/*
		* 函数定义的一种方式之二
		*/
		var aa2 = function(a,b){
			return a*b;
		}
		alert(aa2(4,5));
	</script>
	
	<script>
		/*
		*	构造函数定义JavaScript函数,注意Function中的F是大写
		*/
		var add = new Function('a','b','return a+b;');
		//调用上面定义的add函数
		var sum = add(3,4);
		alert(sum);
	</script>
	
</body>
</html>


可以有返回值,可以用变量来接受其返回值

如果没有return,则返回undefined.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: