const functionName = (arg1, arg2, ?..) => { //body of the function }
参数: 任何函数都可以选择有参数。
粗箭头表示法/lambda 表示法: 这是箭头(=>) 的表示法。
语句: 表示函数的指令集。
// function expression var myfun1 = function show() { console.log("It is a Function Expression"); } // Anonymous function var myfun2 = function () { console.log("It is an Anonymous Function"); } //Arrow function var myfun3 = () => { console.log("It is an Arrow Function"); }; myfun1(); myfun2(); myfun3();
It is a Function Expression It is an Anonymous Function It is an Arrow Function
单个参数的可选括号
var num = x => { console.log(x); } num(140);
140
单条语句的可选大括号,如果不需要任何参数,则为空大括号。
var show = () => console.log("Hello World"); show();
Hello World
var show = (a,b,c) => { console.log(a + " " + b + " " + c ); } show(100,200,300);
100 200 300
var show = (a, b=200) => { console.log(a + " " + b); } show(100);
100 200
var show = (a, b=200) => { console.log(a + " " + b); } show(100,500);
100 500
var show = (a, ...args) => { console.log(a + " " + args); } show(100,200,300,400,500,600,700,800);
100 200,300,400,500,600,700,800
var show = x => { console.log(x); } show("Hello World");
Hello World
function show(value){ return value/2; }
var show = value => value/2; console.log(show(100));
var show = value => { value/2; } console.log(show(50));
undefined
var show = value => { return value/2; } console.log(show(50));
25
this.num.forEach(function(num) { if (num < 30) this.child.push(num); }.bind(this));
this.num.forEach((num) => { if (num < 30) this.child.push(num); });