- JavaScript by Example
- Dani Akash S
- 205字
- 2021-07-02 18:39:07
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.
- 深度實(shí)踐OpenStack:基于Python的OpenStack組件開發(fā)
- 測(cè)試驅(qū)動(dòng)開發(fā):入門、實(shí)戰(zhàn)與進(jìn)階
- Objective-C應(yīng)用開發(fā)全程實(shí)錄
- 計(jì)算機(jī)圖形學(xué)編程(使用OpenGL和C++)(第2版)
- Java編程指南:基礎(chǔ)知識(shí)、類庫(kù)應(yīng)用及案例設(shè)計(jì)
- Python數(shù)據(jù)可視化之Matplotlib與Pyecharts實(shí)戰(zhàn)
- Mastering RStudio:Develop,Communicate,and Collaborate with R
- Getting Started with NativeScript
- Clojure Reactive Programming
- Windows內(nèi)核編程
- SSM開發(fā)實(shí)戰(zhàn)教程(Spring+Spring MVC+MyBatis)
- Getting Started with Eclipse Juno
- 0 bug:C/C++商用工程之道
- Python Data Science Cookbook
- 深度學(xué)習(xí)原理與PyTorch實(shí)戰(zhàn)(第2版)