search
HomeCommon ProblemThe difference between arrow functions and ordinary functions

The difference between arrow functions and ordinary functions

Sep 13, 2023 am 09:32 AM
arrow functionOrdinary 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;

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;

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

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

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

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version