Q. Function Expression
The function
keyword can be used to define a function inside an expression
Syntax
var myFunction = function [name]([param1[, param2[, ..., paramN]]]) {
statements
};
The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions.
You can't use function expressions before you define them
Q. Function Declaration
Syntax
function name([param,[, param,[..., param]]]) {
[statements]
}
A function created with a function declaration is a Function object
You can use the function before you declared it
Q. Arrow Functions
An arrow function expression has a shorter syntax than a function expression
在箭頭函數中,this 指稱的對象在所定義時就固定了
Syntax
Basic Syntax
(param1, param2, …, paramN) => { statements }
(param1, param2, …, paramN) => expression //只有一行(一個expression)時, 連{} 跟 return 都不用
(singleParam) => { statements }
// A function with no parameters should be written with a pair of parentheses.
() => { statements }
Example1:
var add = (a,b) => a + b;
Example 2: 一個參數時, 連{}都不用
var greeting = person => "Hello, " + person;
console.log(greeting("welen"));
Example 3: