- Mastering React Test:Driven Development
- Daniel Irvine
- 191字
- 2021-06-24 14:45:00
Arrow functions
The book almost exclusively uses arrow functions for defining functions. The only exceptions are when we write generator functions, which must use the standard function's syntax. If you’re not familiar with arrow functions, they look like this, which defines a single-argument function named inc:
const inc = arg => arg + 1;
They can appear on one line or broken into two:
const inc = arg =>
arg + 1;
Functions that have more than one argument have the arguments wrapped in brackets:
const add = (a, b) => a+ b;
If a function has multiple statements, then the body is wrapped in curly braces and the return keyword is used to denote when the function returns:
const dailyTimeSlots = (salonOpensAt, salonClosesAt) => {
const totalSlots = (salonClosesAt - salonOpensAt) * 2;
const startTime = new Date().setHours(salonOpensAt, 0, 0, 0);
const increment = 30 * 60 * 1000;
return timeIncrements(totalSlots, startTime, increment);
};
If the function returns an object, then that object must be wrapped in brackets so that the runtime doesn’t think it’s executing a block:
setAppointment(appointment => ({
...appointment,
[name]: value
});
推薦閱讀
- Learning LibGDX Game Development(Second Edition)
- SOA實踐
- 小程序實戰視頻課:微信小程序開發全案精講
- Java EE框架整合開發入門到實戰:Spring+Spring MVC+MyBatis(微課版)
- OpenCV 3和Qt5計算機視覺應用開發
- C#程序設計(慕課版)
- Python爬蟲開發與項目實戰
- Securing WebLogic Server 12c
- Mastering KnockoutJS
- Principles of Strategic Data Science
- 愛上C語言:C KISS
- 零基礎學C語言(第4版)
- Photoshop智能手機APP界面設計
- Oracle Database XE 11gR2 Jump Start Guide
- 讓Python遇上Office:從編程入門到自動化辦公實踐