一般函數(shù)的寫法:
- function 函數(shù)名字(){
- }
箭頭函數(shù)的寫法:
- let 函數(shù)名字=()=>{
- }
下面我們來看一個(gè)例子:
- <script>
- // 一般寫法
- // window.onload=function () {
- // alert('df')
- // }
- // 箭頭函數(shù)寫法
- window.onload=()=>{
- alert('df')
- }
- </script>
- <script>
- // 一般寫法
- /*let show=function () {
- alert('df')
- }
- show()*/
- // 箭頭函數(shù)寫法
- let show=()=>{
- alert('df')
- }
- show()
- </script>
注意:
1)如果只有一個(gè)函數(shù),()可以省略
2)如果只有一個(gè)return,{}可以省略