**函数的声明**
function 函数名(形参){}
function test1( a1, a2){
alert("函数声明1");
}
通过new Function对象声明
var 函数名=new Function("形参","方法体");
var test2=new Function("a1","a2","alert('函数声明2')");
通过赋值function对象来声明
var 变量名=function(形参){方法体} 变量名就是函数名
**函数参数、默认参数**
js的函数调用可以不用赋值,变量有默认值
形参可以不全部赋值
function test4(a1,a2=1)
{
alert("无参赋值");
}
test4(1);
**返回值**
return语句;
无返回值默认返回undefined
var test5=function(){
return "js";
}
alert(test5());
var b=test5();
alert(b);
**到函数运行时函数调用时的所有实参**
arguments //返回类数组
arguments.length 所有实参个数
**函数作为实参进行传递**
function a2(b)
{
alert(b()); //将打印undefined,去掉小括号会打印a3的内容
}
var a3=function()
{
alert("a333");
}
a2(a3);
<html
>
<head
>
<title
>js 函数的学习
</title
>
<meta charset
="utf-8"/>
<script type
="text/javascript">
function test1( a1
, a2
){
alert("函数声明1");
}
var test2
=new Function("a1","a2","alert('函数声明2')");
test2();
alert(test2
);
var test3=function(a1
,a2
){
alert("test3");
}
test3(1,2);
function test4(a1
,a2
)
{
alert("无参赋值");
}
test4();
var test5=function(){
return "js";
}
alert(test5());
var b
=test5();
alert(b
);
function a2(b
)
{
alert(b());
}
var a3=function()
{
alert("a333");
}
a2(a3
);
</script
>
</head
>
<body
>
<h3 align
="center">js 函数的学习
</h3
>
<hr size
="30" color
="aquamarine"/>
</body
>
</html
>