1.匿名函数
var add= function(x,y){ return x+y; } //调用 add(1,2);2.函数作为参数
var multiplication = function(x,y){ return x*y; }; var cal = function(fun,x,y){ return fun(x,y); }; var num = cal(multiplication,5,3);//num = 15;3.参数对象arguments
function adds(){ console.log(arguments.length); var num = 0 ; for (var int = 0; int < arguments.length; int++) { num += arguments[int]; } alert(num); }4.默认参数
function say(name='xiaobai'){ alert("hello,"+name); } say();//hello,xiaobai say("zhangfei");//hello,zhangfei这里eclipse中js文件会报错 5.函数嵌套 一个函数内部可以包含内部函数. 内部函数又再次可以包含内部函数。来看下代码.
function wakeUpAndCode() { function wakeUp() { console.log("I just woke up"); } function code() { console.log("I am ready to code now"); } wakeUp(); code(); } wakeUpAndCode(); // Output: // I just woke up // I am ready to code nowwakeUpAndCode 这个函数包含两个内部函数 wakeUp 和 code. 当调用 wakeUpAndCode 后, 开始执行方法体. 方法体外只有两条执行语句, 分别调用 wakeUp 和 code . 调用 wakeUp 函数会在控制台输出字符 “I just woke up” . 调用 code 函数会在控制台输出字符 “I am ready to code now” . 6.自执行函数
//声明后就会执行 (function() { console.log("I run on my own."); }());7.构造函数 js函数也是js对象
function student(name,age,gender){ this.name = name; this.age = age; this.gender = gender; this.say = function(){ alert('我叫'+name); }; this.sing = function(){ alert(name , age , gender); }; } var s = new student('张飞'); s.say();//我叫张飞 s.sing();//张飞 alert(s);//object