javascript定义函数的方法
javscript中定义和声明函数有三种方式:正常方法 构造函数 函数直接量。
以下是各种方法的示例代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<html>
<head></head>
<body>
<script type="text/javascript">
/*javascript定义函数(声明函数)可以有三种方法:正常方法、构造函数、函数直接量。*/
/*1.正常方法 function(param){}*/
function print(msg)
{
document.write(msg,"<br/>");
}
/*如果函数不包含return 语句,只执行函数体内语句,并返回undefined*/
/*2.构造函数方法:new Function()*/
var add1=new Function('a','b','return a+b');
/*3.函数直接量法,创建未命名函数,*/
var result = function(x,y){return x+y;};
/*也可以指定函数名*/
var result2 = function fact(x){if(x<1) return 1;else return x*fact(x-1)};
document.write('调用一般的方法:');
print("<hr/>");
print('调用构造函数方法:add1(5,6)');
print(add1(5,6));
print("<hr/>");
print("调用函数直接量法:result(3,4)");
var re =result(3,4);
print(re);
print("调用函数直接量法:result2(3)");
print(result2(3));
print("<hr/>");
print('函数作为数据使用');
/*函数可以作为数据使用*/
function add(x,y){return x+y;}
function subtract(x,y){return x-y;}
function multiply(x,y){return x*y;}
function divide(x,y){return x/y;}
function operate(operator,operand1,operand2)
{
return operator(operand1,operand2);
}
//计算(2+3) + (4*5)
var i = operate(add,operate(add,2,3),operate(multiply,4,5));
print('(2+3) + (4*5)='+i);
print("<hr/>");
//使用函数直接量
var operators = new Object();
operators['add'] = function(x,y){return x+y;}
operators['substract'] = function(x,y){return x-y;}
operators['multiply'] = function(x,y){return x*y;}
operators['divide'] = function(x,y){return x/y;}
operators['pow'] = Math.pow;
function operate2(op_name,operand1,operand2)
{
if(operators[op_name] == null) return "unknown operator";
else return operators[op_name](operand1,operand2);
}
//定义"hello" + "" + "world"
var j = operate2("add","hello",operate2("add"," ","world"));
var k = operate2("pow",10,2);
print(j);
print(k);
print("<hr/>");
</script>
</body>
</html>
<head></head>
<body>
<script type="text/javascript">
/*javascript定义函数(声明函数)可以有三种方法:正常方法、构造函数、函数直接量。*/
/*1.正常方法 function(param){}*/
function print(msg)
{
document.write(msg,"<br/>");
}
/*如果函数不包含return 语句,只执行函数体内语句,并返回undefined*/
/*2.构造函数方法:new Function()*/
var add1=new Function('a','b','return a+b');
/*3.函数直接量法,创建未命名函数,*/
var result = function(x,y){return x+y;};
/*也可以指定函数名*/
var result2 = function fact(x){if(x<1) return 1;else return x*fact(x-1)};
document.write('调用一般的方法:');
print("<hr/>");
print('调用构造函数方法:add1(5,6)');
print(add1(5,6));
print("<hr/>");
print("调用函数直接量法:result(3,4)");
var re =result(3,4);
print(re);
print("调用函数直接量法:result2(3)");
print(result2(3));
print("<hr/>");
print('函数作为数据使用');
/*函数可以作为数据使用*/
function add(x,y){return x+y;}
function subtract(x,y){return x-y;}
function multiply(x,y){return x*y;}
function divide(x,y){return x/y;}
function operate(operator,operand1,operand2)
{
return operator(operand1,operand2);
}
//计算(2+3) + (4*5)
var i = operate(add,operate(add,2,3),operate(multiply,4,5));
print('(2+3) + (4*5)='+i);
print("<hr/>");
//使用函数直接量
var operators = new Object();
operators['add'] = function(x,y){return x+y;}
operators['substract'] = function(x,y){return x-y;}
operators['multiply'] = function(x,y){return x*y;}
operators['divide'] = function(x,y){return x/y;}
operators['pow'] = Math.pow;
function operate2(op_name,operand1,operand2)
{
if(operators[op_name] == null) return "unknown operator";
else return operators[op_name](operand1,operand2);
}
//定义"hello" + "" + "world"
var j = operate2("add","hello",operate2("add"," ","world"));
var k = operate2("pow",10,2);
print(j);
print(k);
print("<hr/>");
</script>
</body>
</html>
运行结果为:
调用一般的方法:
调用构造函数方法:add1(5,6)
11
调用函数直接量法:result(3,4)
7
调用函数直接量法:result2(3)
6
函数作为数据使用
(2+3) + (4*5)=25
hello world
100
(注:记录一下这些内容只是以后查阅起来方便,作为自己知识积累的记录。其中有很多是参考网络上的资源,不再一一写出出处,还请原作者见谅。)