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

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
});
主站蜘蛛池模板: 巴林右旗| 乌拉特后旗| 武宁县| 利川市| 成安县| 府谷县| 凌源市| 黔西县| 闻喜县| 洪湖市| 庄河市| 剑川县| 收藏| 津市市| 屏边| 微山县| 共和县| 长丰县| 沙洋县| 牙克石市| 南阳市| 方城县| 唐河县| 怀远县| 大同市| 灌云县| 孟津县| 蓬溪县| 交口县| 苏尼特右旗| 龙海市| 巢湖市| 平原县| 车致| 司法| 长沙市| 田林县| 吉水县| 东阿县| 成安县| 晴隆县|