Function expressions and arrows

函式後面加 () 代表執行程式,沒有 () 代表原始碼。

// function declaration
function sayHi() {
  alert( "Hello" );
}

// function expression
let sayHi = function() {
  alert( "Hello" );
};

function sayHi() {   // (1) create
  alert( "Hello" );
}

let func = sayHi;    // (2) copy

func(); // Hello     // (3) run the copy (it works)!
sayHi(); // Hello    //     this still works too (why wouldn't it)

func f {...}、for {...}、if {....} 都不需要;結尾,但 let f = ... ; 代表一個值需要用;結尾。

Callback functions

帶入函式,預期之後如果需要可以被執行。

Function Expression vs Function Declaration

當執行到的時候,Function Expression 被創造出來, Function Declaration 可以在全域使用。

若函式被宣告在 code blocks 無法作為全域使用。

Arrow functions

expression 不用加 return

多行 arrow functions,需要加 return

Last updated

Was this helpful?