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

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
});
主站蜘蛛池模板: 安国市| 义乌市| 丽水市| 乐业县| 始兴县| 瓦房店市| 吴江市| 西藏| 灵川县| 甘南县| 林州市| 凤阳县| 南溪县| 淮滨县| 赤峰市| 隆德县| 岳阳市| 岢岚县| 苍溪县| 阿拉善盟| 从化市| 萨嘎县| 新昌县| 乌拉特中旗| 西城区| 荣成市| 元谋县| 巴马| 九台市| 杭州市| 临泉县| 肇州县| 台州市| 高密市| 腾冲县| 习水县| 屯门区| 黎城县| 石首市| 开封市| 绥中县|