- 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
});
推薦閱讀
- Kali Linux Web Penetration Testing Cookbook
- HTML5+CSS3+JavaScript Web開發案例教程(在線實訓版)
- Python時間序列預測
- 高級語言程序設計(C語言版):基于計算思維能力培養
- Scala程序員面試算法寶典
- 數據結構與算法分析(C++語言版)
- iOS開發實戰:從入門到上架App Store(第2版) (移動開發叢書)
- Learning Concurrent Programming in Scala
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- Create React App 2 Quick Start Guide
- 基于ARM Cortex-M4F內核的MSP432 MCU開發實踐
- Java SE實踐教程
- 智能搜索和推薦系統:原理、算法與應用
- Python 3 數據分析與機器學習實戰
- INSTANT Apache ServiceMix How-to