function functionName(arg1,arg2){}
<script> sayHi(); function sayHi(){ alert("hi,world") } console.log(sayHi.name) //輸出sayHi,這是sayHi(){} 的name </script>
var functionName = function(arg1,arg2){}
<script> sayBye(); var sayBye = function(){ alert("byebye"); } </script> 報錯:sayBye is not a function
<script> var flag; if(flag){ function sayHi(){ alert('hi'); } }else{ function sayHi(){ alert('love'); } }; sayHi(); //彈出 love </script>
<script> var sayBye; var flag; if(flag){ sayBye = function(){ alert("bye"); } }else{ sayBye = function(){ alert("bye lizi"); } }; sayBye(); //彈出 bye lizi </script> 也能夠這樣寫: <script> function mult(num){ if(num<=0){ return num = 1; }else{ return num * arguments.callee(num-1); } }; alert(mult(3)) ; </script>
<script> 'use strict'; function mult(num){ if(num<=0){ return num = 1; }else{ return num * arguments.callee(num-1); } }; alert(mult(3)) ; //'caller', 'callee', and 'arguments' properties may not be accessed on strict mode //functions </script>
<script> var interation = (function f(num){ if(num <=1){ return num = 1; }else{ return num * f(num-1); } }) alert(interation(3)); </script>