官术网_书友最值得收藏!

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
});
主站蜘蛛池模板: 库尔勒市| 曲沃县| 宜兰市| 大竹县| 嘉黎县| 温宿县| 神池县| 康平县| 都安| 炉霍县| 措勤县| 罗甸县| 吉安县| 呼和浩特市| 澳门| 扎囊县| 晋宁县| 忻州市| 桂林市| 黄冈市| 呼和浩特市| 子洲县| 建德市| 玉屏| 开封市| 灌南县| 类乌齐县| 临朐县| 门头沟区| 社旗县| 岚皋县| 沽源县| 呈贡县| 阿勒泰市| 巴林右旗| 柘城县| 奉新县| 周宁县| 监利县| 太仓市| 海原县|