js動態函式和匿名函式
阿新 • • 發佈:2018-12-23
<html> <head> <title>演示js動態函式匿名函式</title> </head> <body> <!-- <script type="text/javascript"> var fun=function(x,y){ document.write(x+y+"<br>"); return x+y; } // alert(fun(2,3)); document.write(fun(2,3)); </script> --> <!-- 動態函式,依賴於字串, --> <script type="text/javascript"> var add=new Function("x,y","var sum=x+y;return sum;"); alert(add(23,12)); </script> <!-- <input type="button" value="按鈕" id="btn">--> <!-- 匿名函式,一般用在給函式新增事件 --> <button id="btn">一個按鈕</button> <script type="text/javascript"> btn.onclick=function(){ alert("aa"); } </script> </body> </html>