<!DOCTYPE html><html lang="en"><head> < charset="UTF-8"> < >08函数的其他的定义方式</ > < > /** * 命名函数:函数如果有名字,就是命名函数 * * 匿名函数:函数如果没有名字,就是匿名函数 * * 函数的另一种定义方式 * 函数表达式: * 把一个函数给一个变量,此时形成了函数表达式 * var 变量 = 匿名函数; * 例子: * var f1=function (){ * * }; * 如果是函数表达式那么此时前面的变量中存储的就是一个函数,而这个变量就相当于 是一个函数,就可以直接加 * 小括号调用了 * f1(); * * f1即是函数代码块加 (); 就能调用函数 * * 注意: * 函数表达式后面,赋值结束后,要加分号 * * * * * 函数定义: * 1.函数声明--函数定义 * function 函数名(){ * 函数体 * } */ // (function () { // console.log("月色真美!"); // })();//月色真美! //函数的自调用,没有名字,调用---声明的同时,直接调用 //一次性的---- (function(){console.log("月色真美2");})(); var f1=function(){console.log("月色真美2");}; f1(); // //命名函数 // function f1(){ // console.log("哈哈,您又变帅了"); // } // f1();//函数调用 // //如果一个函数能够调用:函数的代码(); // // 匿名函数 // //函数表达式 // var f2=function (){ // console.log("真的真的?"); // }; // //匿名函数不能直接调用 // f2(); // // // var f4=function(){ // console.log("我是一个函数"); // }; // f4(); // //函数声明(如果函数名重名会覆盖,) function f1() { console.log("妹妹教师好帅"); } f1();//姐姐教师好帅(函数重名就执行后一个,后面一个覆盖前面一个) function f1() { console.log("姐姐教师好帅"); } f1();//姐姐教师好帅 // // 函数表达式 var f2=function (){ console.log("助教姐姐好帅"); }; f2();//助教姐姐好帅 f2=function (){ console.log("小助教好帅"); }; f2();//小助教好帅 // var num=10; // console.log(num);//10 // num=100; // console.log(num);//100 </ ></head><body></body></html>