- 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.
- Spring技術(shù)內(nèi)幕:深入解析Spring架構(gòu)與設(shè)計(jì)
- JavaScript:Functional Programming for JavaScript Developers
- 軟件架構(gòu)設(shè)計(jì):大型網(wǎng)站技術(shù)架構(gòu)與業(yè)務(wù)架構(gòu)融合之道
- Learning Laravel 4 Application Development
- 精通Scrapy網(wǎng)絡(luò)爬蟲
- 深入淺出Serverless:技術(shù)原理與應(yīng)用實(shí)踐
- Working with Odoo
- Python項(xiàng)目實(shí)戰(zhàn)從入門到精通
- Building Dynamics CRM 2015 Dashboards with Power BI
- R Data Science Essentials
- Visual C++從入門到精通(第2版)
- Mastering PowerCLI
- Python應(yīng)用開發(fā)技術(shù)
- 體驗(yàn)之道:從需求到實(shí)踐的用戶體驗(yàn)實(shí)戰(zhàn)
- 程序員面試金典(第6版)