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

JavaScript定义函数的方法

2016-11-09 19:57 471 查看
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>form.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>

<body>
<form name="form1" action="test.html" method="post">
<input type="text" name="num1" />
<input type="text" name="num2" />
<input type="button" name="ok" value="保存1"/>
</form>
</body>
<script language="JavaScript">
//方法一使用普通方法
/*
* 普通方法定义函数:
*
*  function 方法名(参数1,参数n){
*
*      方法体;
*
*  }
*
*/
//  function add(a,b){
//      return a+b;
//  }
//
//  alert(add(3,4));

//方法二构造函数方式定义javascript函数
/*
* 构造函数方式定义函数:
*
*  var 变量名 = new Function(参数1,参数n,方法体);
*
*/
//  var add = new Function('a','b','return a+b;');
//
//  alert(add(2,4));

//方法三使用函数直接量的方式定义函数
/*
* 直接量定义函数:
*
*  var 变量名 = function(参数1,参数n){方法体}
*
*/
//  var add = function(a,b){return a+b;}
//
//  var add = function(a,b){
//      return a+b;
//  }
//
//  alert(add(1,4));

</script>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript 函数