1.函数声明
2.return返回值
3.arguments 对象
//无参函数声明 function box(){ alert('调用的时候执行') } box(); //函数调用 //带参函数声明 function box(name,age){ alert('你的姓名是:'+name+',年龄为:'+age); } box('张三',15); //return值函数声明 function box(){ return '返回的值' } function box(){ return; //遇到第一个return时,终止函数不在往下执行 return 100; }
arguments对象
1.函数不解与传递进来多少参数,也不会因为参数不统一而错误,实际上,函数体内可以通过arguments对象来接受传递进来的参数
function box(){ return arguments[0]+' | '+arguments[1]; //得到每次参数的值 } alert(box(1,2,3,4)); //传递参数,这里得到第一位和第二位
2.arguments对象的length属性可以得到参数的长度
function box(){ return arguments.length } alert(box(1,2,3,4)); //4,传递参数
3.利用arguments对象的特殊性,可以动态传参,如下动态传参进行累加(参数数值随便传,长度也任意)
function box(){ var sum; for (var i=0;i<arguments.length;i++) { sum+=arguments[i]; } return sum; } alert(box(1,2,3,4,50)); //60,传递参数进行累加
注:函数不具备重载功能(根据参数,选择相同函数名而参数不同的函数),后面的函数会覆盖前面函数名相同的函数