Home Common Problem The difference between arrow functions and ordinary functions

The difference between arrow functions and ordinary functions

Sep 13, 2023 am 09:32 AM
arrow function Ordinary function

The differences between arrow functions and ordinary functions are mainly in the simplicity of syntax, different this points, not applicable to constructors, no arguments objects, etc. Detailed introduction: 1. Syntax simplicity. The syntax of arrow functions is more concise than that of ordinary functions. Arrow functions can be defined using arrows. The function keyword and curly braces are omitted, and the parameters and return values ​​of the function can be directly defined. The arrow function is When there is only one parameter, the parentheses can also be omitted; 2. This points to different points, etc.

The difference between arrow functions and ordinary functions

Arrow Function and Regular Function are two ways of defining functions in JavaScript. They have some differences in syntax and functionality. . Below I will introduce in detail the difference between arrow functions and ordinary functions.

1. Syntax simplicity:

The syntax of arrow functions is more concise than that of ordinary functions. Arrow functions can be defined using arrows (=>). The function keyword and curly braces are omitted, and the parameters and return values ​​of the function can be directly defined. For example:

   // 普通函数
   function regularFunc(a, b) {
     return a + b;
   }
   
   // 箭头函数
   const arrowFunc = (a, b) => a + b;
Copy after login

The arrow function can also omit the parentheses when it has only one parameter. For example:

   // 普通函数
   function regularFunc(a) {
     return a * 2;
   }
   
   // 箭头函数
   const arrowFunc = a => a * 2;
Copy after login

2. What this points to is different:

In a normal function, the value of this is determined when the function is called, and it points to the object that calls the function. In arrow functions, the value of this is determined when the function is defined, and it points to the context in which the arrow function is defined. This means that the arrow function does not have its own this, it inherits the this of the parent scope. For example:

   // 普通函数
   const obj = {
     name: 'Alice',
     regularFunc: function() {
       console.log(this.name);
     }
   };
   obj.regularFunc(); // 输出:Alice
   
   // 箭头函数
   const obj = {
     name: 'Alice',
     arrowFunc: () => {
       console.log(this.name);
     }
   };
   obj.arrowFunc(); // 输出:undefined
Copy after login

In an arrow function, this points to the context in which the arrow function is defined, not the object that calls the arrow function.

3. Not applicable to constructors:

Arrow functions cannot be used as constructors, and objects cannot be instantiated through the new keyword. Ordinary functions can be used as constructors, and object instances can be created through the new keyword. For example:

   // 普通函数
   function RegularConstructor() {
     this.name = 'Alice';
   }
   const regularObj = new RegularConstructor();
   
   // 箭头函数
   const ArrowConstructor = () => {
     this.name = 'Alice';
   };
   const arrowObj = new ArrowConstructor(); // 报错:ArrowConstructor is not a constructor
Copy after login

4. No arguments object:

In a normal function, you can use the arguments object to access all incoming parameters, which is an array-like object. The arrow function does not have its own arguments object, it inherits the arguments object in the parent scope. For example:

   // 普通函数
   function regularFunc() {
     console.log(arguments[0]);
   }
   regularFunc(1, 2, 3); // 输出:1
   
   // 箭头函数
   const arrowFunc = () => {
     console.log(arguments[0]);
   };
   arrowFunc(1, 2, 3); // 报错:arguments is not defined
Copy after login

In summary, the grammatical difference between arrow functions and ordinary functions is mainly reflected in simplicity and this pointing. Arrow functions have a more concise syntax, but cannot be used as constructors and do not have their own this and arguments objects. The syntax of ordinary functions is relatively complex, but it can be used as a constructor and has its own this and arguments objects. In actual use, we can choose the appropriate function definition method according to specific needs.

The above is the detailed content of The difference between arrow functions and ordinary functions. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

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 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.

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

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.

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

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.

PHP Arrow Functions: How to Simplify Loop Processing PHP Arrow Functions: How to Simplify Loop Processing Sep 13, 2023 am 08:15 AM

PHP Arrow Function: How to Simplify Loop Processing, Need Specific Code Examples Introduction: With the release of PHP7.4, arrow functions have become a very interesting new feature in PHP. The emergence of arrow functions makes us more concise and convenient when dealing with loops. This article will introduce the basic syntax of arrow functions and how to use arrow functions to simplify loop processing, and give specific code examples. The basic syntax of arrow functions The syntax of arrow functions is very simple and can be regarded as a shortcut way of writing anonymous functions. its grammatical structure