1024programmer Java JavaScript functional programming (FunctionalProgramming) arrow function (Arrowfunctions) usage analysis

JavaScript functional programming (FunctionalProgramming) arrow function (Arrowfunctions) usage analysis

The examples in this article describe the usage of Arrow functions in Javascript Functional Programming. Share it with everyone for your reference, the details are as follows:

Arrow functions are in Javascript and were only added in ES6 (ES2015). Because there is an arrow-like symbol in the function: =>, it is called an arrow function. It is also often called Fat arrow functions in English. This kind of function is also called a lambda expression. Arrow functions cannot be used as constructors.

Grammar

An arrow function looks like this:

 const greet = () => hello

 

The left side of the arrow (=>) is the parameters of the function. If the function has no parameters, a set of blank brackets must be used. If the function has only one parameter, there is no need for brackets around the parameter. If there are multiple parameters, these parameters must be Put them in a set of brackets, separated by commas.

The right side of the arrow is the main part of the function. The main body of the above function is only one line, so you can directly place the main body on the right side of the arrow. The arrow function will automatically return the result of this single-line main body, that is, you do not need to explicitly Use the return keyword to return a value. If the body of a function is multi-line, you can enclose the body in a set of braces ({ }).

Single parameter

 const greet = name => `hello, ${name}`

 

name is a parameter of the greet arrow function. Because the function has only one parameter, there is no need to add parentheses around it.

Multiple parameters

 const greet = (greeting, name) => `${greeting}, ${name}`

 

greet This function now has two parameters: greeting and name, which are surrounded by a set of parentheses and separated by commas.

Subject

The body of the above function has only one line, which is an expression. The result of this expression will be returned automatically, which is equivalent to this:

 const greet = (greeting, name) => {
  return `${greeting}, ${name}`
 }

 

The body part of the greet function above uses a set of curly brackets, so in the body you must explicitly use return to return the required results.

For more content related to Javascript, please check out the special topics on this site: “Summary of common functions and skills in Javascript”, “Introduction to object-oriented Javascript tutorial”, “Summary of Javascript errors and debugging skills”, “Summary of Javascript data structure and algorithm skills” and “Summary of Javascript Mathematical Operation Usage”

I hope this article will be helpful to everyone in Javascript programming.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/714801

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索