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

Arrow functions

Arrow functions are a cleaner and shorter way to define functions in JavaScript and they simply inherit the this object of its parent instead of binding its own. We'll see more about the this binding soon. Let's just look into using the new syntax. Consider the following functions:

let a = function(x) {
}
let b = function(x, y) {
}

The equivalent arrow functions can be written as:

let a = x => {}
let b = (x,y) => {}

You can see that () are optional, when we have to pass the only single argument to the function. 

Sometimes, we just return a value in a single line in our functions, such as:

let sum = function(x, y) {
return x + y;
}

If we want to directly return a value in our arrow function in a single line, we can directly ignore the return keyword and {} curly braces and write it as:

let sum = (x, y) => x+y;

That's it! It will automatically return the sum of x and y. However, this can be used only when you want to return the value immediately in a single line.

主站蜘蛛池模板: 四平市| 建始县| 衡山县| 金昌市| 苗栗县| 沈阳市| 常熟市| 平和县| 分宜县| 庄河市| 武鸣县| 定陶县| 东丰县| 巴南区| 灵丘县| 马尔康县| 佛冈县| 乌拉特前旗| 贵溪市| 清徐县| 永州市| 黄浦区| 永州市| 平谷区| 虞城县| 平谷区| 鞍山市| 普兰店市| 宣恩县| 望城县| 革吉县| 酉阳| 治县。| 金堂县| 沙湾县| 广灵县| 五家渠市| 阳高县| 鸡泽县| 仙游县| 浠水县|