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

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
});
主站蜘蛛池模板: 济源市| 灌云县| 潼关县| 临沭县| 棋牌| 略阳县| 山丹县| 黑水县| 成武县| 廊坊市| 尼玛县| 图们市| 宁强县| 长岭县| 武隆县| 台东市| 兰溪市| 册亨县| 望奎县| 温泉县| 安义县| 拉萨市| 江门市| 同仁县| 读书| 鄂托克前旗| 黄陵县| 凉山| 平顺县| 文成县| 滨州市| 乐山市| 翁源县| 南华县| 蒙自县| 改则县| 屏山县| 南江县| 玉龙| 东安县| 嘉荫县|