Table of Contents
Arrow functions
Home Web Front-end Front-end Q&A What is arrow function

What is arrow function

Apr 13, 2021 am 11:32 AM
arrow function

This article will introduce you to the arrow function of es6. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What is arrow function

Arrow functions

ES6 adds the ability to define syntax function expressions using arrow syntax. To a large extent, function objects instantiated by arrow functions behave the same as function objects created by formal function expressions. Arrow functions can be used whenever a function expression is used

    let fn = (a, b) => {
        return a + b    }

    let fun = function (a, b) {
        return a + b    }

    console.log(fn(1, 2))   // 3
    console.log(fun(1, 2))  // 3
Copy after login

Arrow functions are suitable for embedded functions

    let arr = [1, 2, 3, 4];
    console.log(
        arr.map(function (item) {
            return item + 1
        })
    )   // [2,3,4,5]

    console.log(
        arr.map((item) => {
            return item + 1
        })
    )   // [2,3,4,5]
Copy after login

Arrow functions If there is only one parameter, the parentheses can be omitted

    let a = (i) => {
        console.log(i)
    }
    // 当前箭头函数只有一个参数,所以可以省略括号变成以下写法,
    let a = i => {
        console.log(i)
    }
Copy after login

If the arrow function has multiple parameters, use commas to separate them.

    // 如果有多个参数,中间用逗号隔开
    let a = (i, j, k) => {
        console.log(i, j, k)
    }
Copy after login

If there are no parameters, parentheses must be added

    // 如果没有参数,则圆括号必须加
    let a = () => {
        console.log("箭头函数没有参数")
    }
Copy after login

The arrow function can also omit the curly braces, but this will change The behavior of the function is the same as that of a regular function using curly braces. Multiple statements can be written in the function body. If there are no curly braces, it means that there is only one statement after the arrow, and the value of this statement is implicitly returned

    let a = () => {
        console.log("我是带花括号的箭头函数")
        console.log("我是带花括号的箭头函数")
        console.log("我是带花括号的箭头函数")
    }

    let a = () => console.log("我是不带花括号的箭头函数")

    let b = i => i + 1
    console.log(b(1))   // 这个时候箭头函数隐式的返回了i+1的值,所以打印结果为2
Copy after login

Although the syntax of the arrow function is simple, there are still many scenarios where it is not suitable for use. The arrow function cannot use arguments, super and target, nor can it be used as a constructor. The arrow function does not have a prototype attribute.

Recommended learning: Javascript video tutorial

The above is the detailed content of What is arrow function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP arrow functions to implement currying of functions How to use PHP arrow functions to implement currying of functions Sep 13, 2023 am 11:12 AM

How to use PHP arrow functions to implement currying of functions Currying (Currying) is a functional programming concept, which refers to the process of converting a multi-parameter function into a function sequence that only accepts a single parameter. In PHP, we can use arrow functions to implement currying of functions, making the code more concise and flexible. The so-called arrow function is a new anonymous function syntax introduced in PHP7.4. Its characteristic is that it can capture external variables and has only one expression as the function body.

What is the difference between arrow functions and ordinary functions in es6 What is the difference between arrow functions and ordinary functions in es6 Mar 08, 2022 pm 12:11 PM

Differences: 1. The definition of the arrow function is much simpler, clearer and faster than the definition of the ordinary function; 2. The arrow function does not create its own this, but the ordinary function does; 3. The arrow function cannot be used as a constructor, while the arrow function cannot be used as a constructor. Functions can be used as constructors; 4. Arrow functions do not have their own arguments, but arrow functions do.

How to use PHP arrow functions to improve the performance of your code How to use PHP arrow functions to improve the performance of your code Sep 13, 2023 am 10:55 AM

How to use PHP arrow functions to improve code performance requires specific code examples. In PHP 7.4 version, arrow functions (ArrowFunctions) were introduced, which is a more concise anonymous function syntax that can help us improve the performance and readability of code. sex. This article will introduce how to use arrow functions to write efficient PHP code and provide specific code examples. Reduce the cost of function definition. The traditional anonymous function definition method will introduce certain overhead, including the definition of function name and closure loop.

How to use PHP arrow functions to simplify conditional statements How to use PHP arrow functions to simplify conditional statements Sep 13, 2023 am 09:40 AM

How to use PHP arrow functions to simplify conditional statements In PHP programming, we often need to use conditional statements (if-else) to execute different code blocks based on different conditions. However, using traditional if-else syntax can make the code cumbersome and difficult to read. To simplify this process, PHP7.4 introduced arrow functions (arrowfunctions). Arrow functions provide a more concise and easy-to-read way to write conditional statements. This article will introduce the arrow function

Explain Arrow Functions (short closures) introduced in PHP 7.4. Explain Arrow Functions (short closures) introduced in PHP 7.4. Apr 06, 2025 am 12:01 AM

The arrow function was introduced in PHP7.4 and is a simplified form of short closures. 1) They are defined using the => operator, omitting function and use keywords. 2) The arrow function automatically captures the current scope variable without the use keyword. 3) They are often used in callback functions and short calculations to improve code simplicity and readability.

What does this point to in the es6 arrow method? What does this point to in the es6 arrow method? Nov 21, 2022 pm 05:55 PM

In es6, the this object in the arrow function body is the object pointed to by the scope in which the function is defined. The point of this in the arrow function is the point of the object in the context. Occasionally, if there is no context object, this points to the window; even call, apply, bind and other methods cannot change the point of this of the arrow function.

PHP Arrow Functions: How to handle nested calls to higher-order functions PHP Arrow Functions: How to handle nested calls to higher-order functions Sep 13, 2023 am 08:27 AM

PHP arrow functions: How to handle nested calls of higher-order functions, specific code examples are needed Introduction: In PHP7.4 version, the concept of arrow functions (arrowfunctions) was introduced. Arrow functions are a concise way of writing and can be processed elegantly. Nested calls to higher-order functions. This article will introduce the basic use of arrow functions and demonstrate how to handle nested calls of higher-order functions through specific code examples. 1. What is an arrow function? Arrow function is a new feature introduced in PHP7.4 version. It is a

This article will teach you about JS arrow functions This article will teach you about JS arrow functions Nov 10, 2022 pm 03:58 PM

This article can let you know all about JavaScript arrow functions. We’ll show you how to use ES6’s arrow syntax, as well as some common mistakes to watch out for when using arrow functions in your code. You'll see lots of examples of how they work.

See all articles