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

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
});
主站蜘蛛池模板: 新营市| 常山县| 高淳县| 六盘水市| 许昌市| 赤城县| 陈巴尔虎旗| 黎川县| 陵水| 平顺县| 墨玉县| 盖州市| 沅陵县| 河源市| 定远县| 枣庄市| 霍邱县| 西充县| 鹰潭市| 息烽县| 洪洞县| 平陆县| 大竹县| 泽普县| 安康市| 明溪县| 永清县| 永和县| 嫩江县| 凤冈县| 合水县| 贞丰县| 海宁市| 黑山县| 逊克县| 什邡市| 宝清县| 民县| 日喀则市| 黎川县| 玉山县|